function UpdateSettingsForm::validateForm

Same name and namespace in other branches
  1. 8.9.x core/modules/update/src/UpdateSettingsForm.php \Drupal\update\UpdateSettingsForm::validateForm()

Overrides FormBase::validateForm

File

core/modules/update/src/UpdateSettingsForm.php, line 119

Class

UpdateSettingsForm
Configure update settings for this site.

Namespace

Drupal\update

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $form_state->set('notify_emails', []);
  if (!$form_state->isValueEmpty('update_notify_emails')) {
    $valid = [];
    $invalid = [];
    foreach (explode("\n", trim($form_state->getValue('update_notify_emails'))) as $email) {
      $email = trim($email);
      if (!empty($email)) {
        if ($this->emailValidator
          ->isValid($email)) {
          $valid[] = $email;
        }
        else {
          $invalid[] = $email;
        }
      }
    }
    if (empty($invalid)) {
      $form_state->set('notify_emails', $valid);
    }
    elseif (count($invalid) == 1) {
      $form_state->setErrorByName('update_notify_emails', $this->t('%email is not a valid email address.', [
        '%email' => reset($invalid),
      ]));
    }
    else {
      $form_state->setErrorByName('update_notify_emails', $this->t('%emails are not valid email addresses.', [
        '%emails' => implode(', ', $invalid),
      ]));
    }
  }
  parent::validateForm($form, $form_state);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.