You are here

public function PushNotificationsSendMessageForm::validateForm in Push Notifications 8

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/PushNotificationsSendMessageForm.php, line 69

Class

PushNotificationsSendMessageForm
Class PushNotificationsSendMessageForm.

Namespace

Drupal\push_notifications\Form

Code

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

  // Make sure at least one network is selected.
  $networks = $form_state
    ->getValue('networks');
  if (empty(array_filter($networks))) {
    $form_state
      ->setErrorByName('networks', $this
      ->t('Please select at least one of the target networks.'));
  }

  // Determine recipients.
  $tokens = push_notifications_get_tokens(array(
    'networks' => $networks,
  ));
  if (empty($tokens)) {

    // Onlyproceed if tokens were found.
    $form_state
      ->setErrorByName('networks', $this
      ->t('No tokens found for your selected networks.'));
  }
  else {

    // Pass the tokens to the submit handler.
    $form_state
      ->setTemporaryValue('tokens', $tokens);
  }
  parent::validateForm($form, $form_state);
}