You are here

public function ProtectedPagesSendEmailForm::validateForm in Protected Pages 8

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

src/Form/ProtectedPagesSendEmailForm.php, line 172

Class

ProtectedPagesSendEmailForm
Provides send protected pages details email form.

Namespace

Drupal\protected_pages\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $emails = explode(',', str_replace([
    "\r",
    "\n",
  ], ',', $form_state
    ->getValue('recipents')));
  foreach ($emails as $key => $email) {
    $email = trim($email);
    if ($email) {
      if (!$this->emailValidator
        ->isValid($email)) {
        $form_state
          ->setErrorByName('recipents', $this
          ->t('Invalid email address: @mail. Please correct this email.', [
          '@mail' => $email,
        ]));
        unset($emails[$key]);
      }
      else {
        $emails[$key] = $email;
      }
    }
    else {
      unset($emails[$key]);
    }
  }
  $form_state
    ->set('validated_recipents', implode(', ', $emails));
}