You are here

public static function YamlFormEmailMultiple::validateYamlFormEmailMultiple in YAML Form 8

Form element validation handler for #type 'email_multiple'.

File

src/Element/YamlFormEmailMultiple.php, line 45

Class

YamlFormEmailMultiple
Provides a form element for entering multiple comma delimited email addresses.

Namespace

Drupal\yamlform\Element

Code

public static function validateYamlFormEmailMultiple(&$element, FormStateInterface $form_state, &$complete_form) {
  $value = trim($element['#value']);
  $form_state
    ->setValueForElement($element, $value);
  if ($value) {
    $values = preg_split('/\\s*,\\s*/', $value);
    foreach ($values as $value) {

      // Allow tokens to be be include in multiple email list.
      if (!empty($element['#allow_tokens'] && preg_match('/^\\[.*\\]$/', $value))) {
        continue;
      }
      if (!\Drupal::service('email.validator')
        ->isValid($value)) {
        $form_state
          ->setError($element, t('The email address %mail is not valid.', [
          '%mail' => $value,
        ]));
        return;
      }
    }
  }
}