How do I alter/change the "field is required" message? I have below code

$form['name'] = [
      '#type' =&gt; 'textfield',
      //'#title' =&gt; $this-&gt;t('Email'),
      '#size' =&gt; 60,
      '#maxlength' =&gt; USERNAME_MAX_LENGTH,
      '#default_value' =&gt; $form_state-&gt;getValue('name', ''),
      '#required' =&gt; TRUE,
      '#attributes' =&gt; [
        'placeholder' =&gt; $this-&gt;t('Email'),
        'autocorrect' =&gt; 'none',
        'autocapitalize' =&gt; 'none',
        'spellcheck' =&gt; 'false',
        'autofocus' =&gt; '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-&gt;isValueEmpty('name')) {
  $form_state-&gt;setErrorByName('name', $this-&gt;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.
Try this, it might work. 

$name = $form_state-&gt;getValue('name');
if (empty($name)) {
   $form_state-&gt;setErrorByName('name', $this-&gt;t('Email is required.'));
}