You are here

public function DisclaimerEmailMatchForm::validateForm in Disclaimer 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/DisclaimerEmailMatchForm.php, line 96

Class

DisclaimerEmailMatchForm
Class DisclaimerEmailMatchForm.

Namespace

Drupal\disclaimer\Form

Code

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

  // Don't validate anything when leaving.
  if ($form_state
    ->getTriggeringElement()['#name'] == 'leave') {
    return;
  }
  parent::validateForm($form, $form_state);
  $this
    ->populateSettings($form, $form_state);

  // Block ID is essential for this form.
  if (empty($form_state
    ->getValue('block_id')) || empty($this->blockSettings['email_validation_fail'])) {

    // Provide some feedback.
    $form_state
      ->setErrorByName('', $this
      ->t('Unable to verify the e-mail.'));

    // But this is really backend error.
    // Hint: Hidden block_id value needs to be set in code.
    $this
      ->logger('disclaimer')
      ->error('disclaimer_email_match_form needs to be provided with block_id');
    return;
  }

  // E-mail is required.
  // Can't mark form element as required as it results
  // in error message when "Leave" button is used.
  if (empty($form_state
    ->getValue('email'))) {
    $form_state
      ->setErrorByName('email', $this
      ->t('E-mail field is required.'));
  }

  // Validate e-mail against the list.
  if (!$this
    ->validateEmail($form, $form_state)) {
    $form_state
      ->setErrorByName('email', $this->blockSettings['email_validation_fail']);
  }
}