You are here

public function CSSCaptchaSettingsForm::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

css_captcha/src/Form/CSSCaptchaSettingsForm.php, line 62

Class

CSSCaptchaSettingsForm
CSS CAPTCHA settings form.

Namespace

Drupal\css_captcha\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $css_captcha_allowed_characters = $form_state
    ->getValue('css_captcha_allowed_characters');

  // Checking if there are spaces among allowed characters.
  if (preg_match('/\\s/', $css_captcha_allowed_characters)) {
    $form_state
      ->setErrorByName('css_captcha_allowed_characters', $this
      ->t('The list of characters to use should not contain spaces.'));
  }

  // Checking if number of entered symbols not less than length of code.
  if (strlen($css_captcha_allowed_characters) < $form_state
    ->getValue('css_captcha_code_length')) {
    $form_state
      ->setErrorByName('css_captcha_allowed_characters', $this
      ->t('Number of allowed characters should be at least as code length.'));
  }

  // Checking for duplicated characters among allowed characters.
  if ($css_captcha_allowed_characters !== implode('', array_unique(str_split($css_captcha_allowed_characters)))) {
    $form_state
      ->setErrorByName('css_captcha_allowed_characters', $this
      ->t('Please remove duplicated characters.'));
  }
  parent::validateForm($form, $form_state);
}