You are here

public function MessageForm::validateForm in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/contact/src/MessageForm.php \Drupal\contact\MessageForm::validateForm()

Button-level validation handlers are highly discouraged for entity forms, as they will prevent entity validation from running. If the entity is going to be saved during the form submission, this method should be manually invoked from the button-level validation handler, otherwise an exception will be thrown.

Overrides ContentEntityForm::validateForm

File

core/modules/contact/src/MessageForm.php, line 194

Class

MessageForm
Form controller for contact message forms.

Namespace

Drupal\contact

Code

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

  // Check if flood control has been activated for sending emails.
  if (!$this
    ->currentUser()
    ->hasPermission('administer contact forms') && (!$message
    ->isPersonal() || !$this
    ->currentUser()
    ->hasPermission('administer users'))) {
    $limit = $this
      ->config('contact.settings')
      ->get('flood.limit');
    $interval = $this
      ->config('contact.settings')
      ->get('flood.interval');
    if (!$this->flood
      ->isAllowed('contact', $limit, $interval)) {
      $form_state
        ->setErrorByName('', $this
        ->t('You cannot send more than %limit messages in @interval. Try again later.', [
        '%limit' => $limit,
        '@interval' => $this->dateFormatter
          ->formatInterval($interval),
      ]));
    }
  }
  return $message;
}