You are here

public function ContactFormEditForm::validateForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/contact/src/ContactFormEditForm.php \Drupal\contact\ContactFormEditForm::validateForm()
  2. 10 core/modules/contact/src/ContactFormEditForm.php \Drupal\contact\ContactFormEditForm::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

core/modules/contact/src/ContactFormEditForm.php, line 137

Class

ContactFormEditForm
Base form for contact form edit forms.

Namespace

Drupal\contact

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  // Validate and each email recipient.
  $recipients = explode(',', $form_state
    ->getValue('recipients'));
  foreach ($recipients as &$recipient) {
    $recipient = trim($recipient);
    if (!$this->emailValidator
      ->isValid($recipient)) {
      $form_state
        ->setErrorByName('recipients', $this
        ->t('%recipient is an invalid email address.', [
        '%recipient' => $recipient,
      ]));
    }
  }
  $form_state
    ->setValue('recipients', $recipients);
  $redirect_url = $form_state
    ->getValue('redirect');
  if ($redirect_url && $this->pathValidator
    ->isValid($redirect_url)) {
    if (mb_substr($redirect_url, 0, 1) !== '/') {
      $form_state
        ->setErrorByName('redirect', $this
        ->t('The path should start with /.'));
    }
  }
}