You are here

public function SettingsForm::validateFormEmails in Reroute Email 2.x

Same name and namespace in other branches
  1. 8 src/Form/SettingsForm.php \Drupal\reroute_email\Form\SettingsForm::validateFormEmails()

Validate multiple email addresses field.

Parameters

array $element: A field array to validate.

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

File

src/Form/SettingsForm.php, line 185

Class

SettingsForm
Implements a settings form for Reroute Email configuration.

Namespace

Drupal\reroute_email\Form

Code

public function validateFormEmails(array $element, FormStateInterface $form_state) {

  // Allow only valid email addresses.
  $addresses = reroute_email_split_string($form_state
    ->getValue($element['#name']));
  foreach ($addresses as $address) {
    if (!$this->emailValidator
      ->isValid($address)) {
      $form_state
        ->setErrorByName($element['#name'], $this
        ->t('@address is not a valid email address.', [
        '@address' => $address,
      ]));
    }
  }

  // Save value in usable way to use as `to` param in drupal_mail.
  // String "email@example.com; ;; , ,," save just as "email@example.com".
  // This will be ignored if any validation errors occur.
  $form_state
    ->setValue($element['#name'], implode(',', $addresses));
}