You are here

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

Same name and namespace in other branches
  1. 8.4 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 75

Class

LdapAuthenticationProfileUpdateForm
Profile update form.

Namespace

Drupal\ldap_authentication\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  if (!filter_var($form_state
    ->getValue([
    'mail',
  ]), FILTER_VALIDATE_EMAIL)) {
    $form_state
      ->setErrorByName('mail', $this
      ->t('You must specify a valid email address.'));
  }
  $existing = user_load_by_mail($form_state
    ->getValue([
    'mail',
  ]));
  if ($existing) {
    $form_state
      ->setErrorByName('mail', $this
      ->t('This email address is already in use.'));
  }
  $pattern = $this
    ->configFactory()
    ->get('ldap_authentication.settings')
    ->get('emailTemplateUsagePromptRegex');
  $regex = '`' . $pattern . '`i';
  if (preg_match($regex, $form_state
    ->getValue([
    'mail',
  ]))) {
    $form_state
      ->setErrorByName('mail', $this
      ->t('This email address still matches the invalid email template.'));
  }
}