Form fields still treated as required even after setting #required to false I'm using hook_form_alter to remove the "required" setting for a couple of fields, specifically the First name and Last Name fields on the Address form.

Here's my code, where I provide a callback function for #after_build to remove the required setting:

function mymodule_form_alter(&amp;$form, FormStateInterface $form_state, $form_id) {
  if ($form_id === 'listing_edit_form') {
    $form['address']['widget'][0]['address']['#after_build'][] = '_mymodule_customize_address';
  }
}

function _mymodule_customize_address($element, $form_state) {
  $element['given_name']['#required'] = false;
  $element['family_name']['#required'] = false;
  return $element;
}


The problem is that after I submit the form, the fields are still treated as required, which means that if I don't enter any data for them the submit fails with an error message telling me that those fields are required.

Note that even if I hide the fields by setting #access to false, the form submit will still complain that they are required. So it seems to be a backend issue with the Address module.

I have:

Drupal                  8.7.1
drupal/address          1.6.0
commerceguys/addressing 1.0.3
commerceguys/enum       1.0
commerceguys/intl       1.0.4



I'd appreciate any help with this. Thanks!
Have you tried unset() on the #required element, rather than making it false?