Drop UserMailRequired constraint In my Drupal site, only administrators create users. And users are created without an email. Users can edit themselves some of their account fields, but the email is not one of them.

But the user module adds a UserMailRequired constraint to the User entity email field. On submit, the user module's validator UserMailRequiredValidator adds a violation if the current user is not an administrator :

class UserMailRequiredValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($items, Constraint $constraint) {
    /** @var \Drupal\Core\Field\FieldItemListInterface $items */
    /** @var \Drupal\user\UserInterface $account */
    $account = $items-&gt;getEntity();
    $existing_value = NULL;
    if ($account-&gt;id()) {
      $account_unchanged = \Drupal::entityManager()
        -&gt;getStorage('user')
        -&gt;loadUnchanged($account-&gt;id());
      $existing_value = $account_unchanged-&gt;getEmail();
    }

    $required = !(!$existing_value &amp;&amp; \Drupal::currentUser()-&gt;hasPermission('administer users'));

    if ($required &amp;&amp; (!isset($items) || $items-&gt;isEmpty())) {
      $this-&gt;context-&gt;addViolation($constraint-&gt;message, ['@name' =&gt; $account-&gt;getFieldDefinition('mail')-&gt;getLabel()]);
    }
  }

}


So, none of my user can edit its profile. On submit, they get an error message "Email field is required". Even if the field's #required property is set to false (using hook_form_alter) and the field not displayed (#access = false) in the form render array.

How can I drop this constraint from a custom module ? (using a hook for example)
For those that find themselves here trying to make User Emails Optional, use hook_validation_constraint_alter. You're not extending core's, you're creating a new constraint and using it instead of core's. In my case, I replace core's with... an empty function to achieve what I want.

Something like this in .module

function optional_account_email_validation_constraint_alter(array &amp;$definitions) {
  $definitions['UserMailRequired']['class'] = '\Drupal\my_module_name\Constraint\OverrideUserMailRequired';
}


Then a copy of the two UserMailRequiredValidator.php and UserMailRequired.php files changed to your liking.

The module administerusersbyrole has a working example of this. 

I'll see if I can get around to putting my custom module onto drupal.org in the coming months.