I have below code
$form['name'] = [
'#type' => 'textfield',
//'#title' => $this->t('Email'),
'#size' => 60,
'#maxlength' => USERNAME_MAX_LENGTH,
'#default_value' => $form_state->getValue('name', ''),
'#required' => TRUE,
'#attributes' => [
'placeholder' => $this->t('Email'),
'autocorrect' => 'none',
'autocapitalize' => 'none',
'spellcheck' => 'false',
'autofocus' => 'autofocus',
],
];
When I comment title it shows blank. I would like to change error message of the required field.
In the validation handler, I tried with this code.
if ($form_state->isValueEmpty('name')) {
$form_state->setErrorByName('name', $this->t('Email is required.'));
}
It's not showing the error message.
I would like to keep the form element required and alter the error message.