You are here

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

image_captcha/src/Form/ImageCaptchaSettingsForm.php, line 255

Class

ImageCaptchaSettingsForm
Displays the pants settings form.

Namespace

Drupal\image_captcha\Form

Code

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

  // Check image_captcha_image_allowed_chars for spaces.
  if (preg_match('/\\s/', $form_state
    ->getValue('image_captcha_image_allowed_chars'))) {
    $form_state
      ->setErrorByName('image_captcha_image_allowed_chars', $this
      ->t('The list of characters to use should not contain spaces.'));
  }
  if (!isset($form['image_captcha_font_settings']['no_ttf_support'])) {

    // Check the selected fonts.
    // Filter the image_captcha fonts array to pick out the selected ones.
    $fonts = array_filter($form_state
      ->getValue('image_captcha_fonts'));
    if (count($fonts) < 1) {
      $form_state
        ->setErrorByName('image_captcha_fonts', $this
        ->t('You need to select at least one font.'));
    }
    if ($form_state
      ->getValue('image_captcha_fonts')['BUILTIN']) {

      // With the built in font, only latin2 characters should be used.
      if (preg_match('/[^a-zA-Z0-9]/', $form_state
        ->getValue('image_captcha_image_allowed_chars'))) {
        $form_state
          ->setErrorByName('image_captcha_image_allowed_chars', $this
          ->t('The built-in font only supports Latin2 characters. Only use "a" to "z" and numbers.'));
      }
    }
    $readable_fonts = [];
    $available_fonts = _image_captcha_get_font_uri();
    foreach ($fonts as $token) {
      $fonts[$token] = $available_fonts[$token];
    }
    list($readable_fonts, $problem_fonts) = _image_captcha_check_fonts($fonts);
    if (count($problem_fonts) > 0) {
      $form_state
        ->setErrorByName('image_captcha_fonts', $this
        ->t('The following fonts are not readable: %fonts.', [
        '%fonts' => implode(', ', $problem_fonts),
      ]));
    }
  }

  // Check color settings.
  if (!preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $form_state
    ->getValue('image_captcha_background_color'))) {
    $form_state
      ->setErrorByName('image_captcha_background_color', $this
      ->t('Background color is not a valid hexadecimal color value.'));
  }
  if (!preg_match('/^#([0-9a-fA-F]{3}){1,2}$/', $form_state
    ->getValue('image_captcha_foreground_color'))) {
    $form_state
      ->setErrorByName('image_captcha_foreground_color', $this
      ->t('Text color is not a valid hexadecimal color value.'));
  }
  parent::validateForm($form, $form_state);
}