You are here

protected function PetPreviewForm::validateRecipients in Previewable email templates 8.3

Validate existence of a non-empty recipient list free of email errors.

@todo: make more generic, move specific parts to validateForm(). @todo: test recipient callback part.

Parameters

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

array $recipients_array: Array to hold the $recipients as array, incl uid.

Return value

array Array of errors.

1 call to PetPreviewForm::validateRecipients()
PetPreviewForm::validateForm in src/Form/PetPreviewForm.php
Form validation handler.

File

src/Form/PetPreviewForm.php, line 359

Class

PetPreviewForm
PetPreviewForm.

Namespace

Drupal\pet\Form

Code

protected function validateRecipients(FormStateInterface &$form_state, array &$recipients_array) {
  $errors = [];
  $recipients_array = [];
  if ($form_state
    ->getValue('recipient_callback')) {

    // todo: test.
    $items = $this
      ->callbackRecipients($form_state);
    if (!empty($mails)) {
      $errors[] = $this
        ->t('There is no recipient callback defined for this template or it is not returning an array.');
      return $errors;
    }
  }
  else {

    // Get recipients from form field.
    $recipients = explode(',', $form_state
      ->getValue('recipients'));
  }

  // Validate and build recipient array with uid on the fly.
  foreach ($recipients as $recipient) {
    $recipient = Xss::filter(trim($recipient));
    if (!\Drupal::service('email.validator')
      ->isValid($recipient)) {
      $errors[] = $this
        ->t('Invalid email address found: %mail.', [
        '%mail' => $recipient,
      ]);
    }
    else {
      $recipients_array[$recipient] = [
        'uid' => $this
          ->getUidFromEmailAddress($recipient),
      ];
    }
  }

  // Check for no recipients.
  if (empty($errors) && count($recipients_array) < 1) {
    $errors[] = $this
      ->t('There are no recipients for this email.');
  }
  return $errors;
}