You are here

public function MassContactForm::validateForm in Mass Contact 8

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

src/Form/MassContactForm.php, line 377

Class

MassContactForm
Main form for sending Mass Contact emails.

Namespace

Drupal\mass_contact\Form

Code

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

  // Get the list of recipients for all chosen categories if any have been
  // chosen.
  $field_categories_value = array_filter($form_state
    ->getValue('categories'));
  if (!empty($field_categories_value)) {
    $categories = $this->entityTypeManager
      ->getStorage('mass_contact_category')
      ->loadMultiple($field_categories_value);
    $all_recipients = $this->massContact
      ->getRecipients($categories, $form_state
      ->getValue('optout'));
  }

  // If the 'Send yourself a copy' option has been chosen. count it as a
  // recipient.
  if ($form_state
    ->getValue('copy')) {
    $all_recipients[] = $this
      ->currentUser()
      ->id();
  }

  // Either a category should be chosen, or send yourself a copy option should
  // be checked.
  if (empty($all_recipients)) {
    $form_state
      ->setErrorByName('categories', $this
      ->t('There are no recipients chosen for this mass contact message.'));
  }
}