You are here

public function ForwardForm::validateForm in Forward 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::validateForm()
  2. 8 src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::validateForm()
  3. 8.2 src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::validateForm()
  4. 4.x src/Form/ForwardForm.php \Drupal\forward\Form\ForwardForm::validateForm()

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/ForwardForm.php, line 407

Class

ForwardForm
Forward a page to a friend.

Namespace

Drupal\forward\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  if (!$this
    ->currentUser()
    ->hasPermission('override flood control')) {
    $event = $this
      ->getFloodControlEventName();
    if (!$this->floodInterface
      ->isAllowed($event, $this->settings['forward_flood_control_limit'])) {
      $message = new FormattableMarkup($this->settings['forward_flood_control_error'], [
        '@number' => $this->settings['forward_flood_control_limit'],
      ]);
      $form_state
        ->setErrorByName('', $message);
    }
  }
  $recipients = $this
    ->splitEmailAddresses($form_state
    ->getValue('recipient'));
  if (count($recipients) > $this->settings['forward_max_recipients']) {
    $message = $this
      ->getStringTranslation()
      ->formatPlural($this->settings['forward_max_recipients'], "You can't send email to more than 1 recipient at a time.", "You can't send email to more than @count recipients at a time.");
    $form_state
      ->setErrorByName('recipient', $message);
  }
  foreach ($recipients as $recipient) {
    if (!$this->emailValidator
      ->isValid($recipient)) {
      $message = $this
        ->t('The email address %mail is not valid.', [
        '%mail' => $recipient,
      ]);
      $form_state
        ->setErrorByName('recipient', $message);
    }
  }
  parent::validateForm($form, $form_state);
}