You are here

public function HoneypotSettingsController::validateForm in Honeypot 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/HoneypotSettingsController.php \Drupal\honeypot\Controller\HoneypotSettingsController::validateForm()

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/Controller/HoneypotSettingsController.php, line 279

Class

HoneypotSettingsController
Returns responses for Honeypot module routes.

Namespace

Drupal\honeypot\Controller

Code

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

  // Make sure the time limit is a positive integer or 0.
  $time_limit = $form_state
    ->getValue('time_limit');
  if (is_numeric($time_limit) && $time_limit > 0 || $time_limit === '0') {
    if (ctype_digit($time_limit)) {

      // Good to go.
    }
    else {
      $form_state
        ->setErrorByName('time_limit', $this
        ->t("The time limit must be a positive integer or 0."));
    }
  }
  else {
    $form_state
      ->setErrorByName('time_limit', $this
      ->t("The time limit must be a positive integer or 0."));
  }

  // Make sure Honeypot element name only contains A-Z, 0-9.
  if (!preg_match("/^[-_a-zA-Z0-9]+\$/", $form_state
    ->getValue('element_name'))) {
    $form_state
      ->setErrorByName('element_name', $this
      ->t("The element name cannot contain spaces or other special characters."));
  }

  // Make sure Honeypot element name starts with a letter.
  if (!preg_match("/^[a-zA-Z].+\$/", $form_state
    ->getValue('element_name'))) {
    $form_state
      ->setErrorByName('element_name', $this
      ->t("The element name must start with a letter."));
  }

  // Make sure Honeypot element name isn't one of the reserved names.
  $reserved_element_names = [
    'name',
    'pass',
    'website',
  ];
  if (in_array($form_state
    ->getValue('element_name'), $reserved_element_names)) {
    $form_state
      ->setErrorByName('element_name', $this
      ->t("The element name cannot match one of the common Drupal form field names (e.g. @names).", [
      '@names' => implode(', ', $reserved_element_names),
    ]));
  }
}