You are here

public function LdapAuthenticationProfileUpdateForm::validateForm in Lightweight Directory Access Protocol (LDAP) 8.4

Same name and namespace in other branches
  1. 8.3 ldap_authentication/src/Form/LdapAuthenticationProfileUpdateForm.php \Drupal\ldap_authentication\Form\LdapAuthenticationProfileUpdateForm::validateForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

ldap_authentication/src/Form/LdapAuthenticationProfileUpdateForm.php, line 93

Class

LdapAuthenticationProfileUpdateForm
Profile update form.

Namespace

Drupal\ldap_authentication\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) : void {
  if (!filter_var($form_state
    ->getValue([
    'mail',
  ]), FILTER_VALIDATE_EMAIL)) {
    $form_state
      ->setErrorByName('mail', $this
      ->t('You must specify a valid email address.'));
  }
  $users = $this->entityTypeManager
    ->getStorage('user')
    ->loadByProperties([
    'mail' => $form_state
      ->getValue([
      'mail',
    ]),
  ]);
  if (count($users) > 0) {
    $form_state
      ->setErrorByName('mail', $this
      ->t('This email address is already in use.'));
  }
  $regex = sprintf('`%s`i', $this
    ->configFactory()
    ->get('ldap_authentication.settings')
    ->get('emailTemplateUsagePromptRegex'));
  if (preg_match($regex, $form_state
    ->getValue([
    'mail',
  ]))) {
    $form_state
      ->setErrorByName('mail', $this
      ->t('This email address still matches the invalid email template.'));
  }
}