You are here

public function MathCaptchaSettingsForm::validateForm in CAPTCHA Pack 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

math_captcha/src/Form/MathCaptchaSettingsForm.php, line 151

Class

MathCaptchaSettingsForm
Math CAPTCHA settings form.

Namespace

Drupal\math_captcha\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $enabled_challenges = $form_state
    ->getValue('math_captcha_enabled_challenges');
  if (count(array_filter($enabled_challenges)) < 1) {
    $form_state
      ->setErrorByName('math_captcha_enabled_challenges', $this
      ->t('You should select at least one type of math challenges.'));
  }
  $challenges = array_keys($enabled_challenges);
  foreach ($challenges as $challenge) {
    if (empty($enabled_challenges[$challenge])) {
      continue;
    }
    $argmax = "math_captcha_{$challenge}_argmax";
    if (!ctype_digit($form_state
      ->getValue($argmax))) {
      $form_state
        ->setErrorByName($argmax, $this
        ->t('Maximum value should be an integer.'));
    }
    else {
      $form_state
        ->setValue($argmax, intval($form_state
        ->getValue($argmax)));
      if ($form_state
        ->getValue($argmax) < 2) {
        $form_state
          ->setErrorByName($argmax, $this
          ->t('Maximum value should be an integer and at least 2'));
      }
    }
  }
  parent::validateForm($form, $form_state);
}