How to modify if a field is required in a form? Use case: I have added a required field for users. When user 1 (or an administrator) adds a user, the field should not be required. 

Code that doesn't work (in Drupal 8):

function mymodule_form_alter(&amp;$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'user_register_form') {
    if (\Drupal::currentUser()-&gt;id() == 1) {
      $form['field_fieldname']['#required'] = FALSE; // No effect
      $form['field_fieldname']['widget']['#required'] = FALSE; 
    }
  }
}


Changing the widget "works", but the validation fails. The solution for Drupal 7 doesn't seem to be usable anymore.

$form['field_fieldname'][&lt;lang&gt;][0]['value']['#required'] = FALSE;
It can be tricky to bybass D8 entity validation scheme in this way.
Why don't you just set this field settings as "not required" and then set

$form['field_fieldname']['widget'][0]['#required'] = TRUE


when  

\Drupal::currentUser()-&gt;id() != 1