You are here

public function CookiesConfigForm::validateForm in COOKiES Consent Management 1.0.x

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/CookiesConfigForm.php, line 203

Class

CookiesConfigForm
Form to configure the COOKiES module settings.

Namespace

Drupal\cookies\Form

Code

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

  // Validate cookie name.
  if (($value = $form_state
    ->getValue('cookie_name')) && preg_match('/^[a-zA-Z0-9_]{3,64}$/', $value) !== 1) {
    $form_state
      ->setErrorByName('cookie_name', $this
      ->t("The cookie name has invalid characters. Permitted are numbers, letters and underscores."));
  }

  // Validate cookie domain pattern.
  $pattern = '/^(?!\\-)(?:(?:[a-zA-Z\\d][a-zA-Z\\d\\-]{0,61})?[a-zA-Z\\d]\\.){1,126}(?!\\d+)[a-zA-Z\\d]{1,63}$/';
  if (($value = $form_state
    ->getValue('cookie_domain')) && preg_match($pattern, $value) !== 1) {
    $form_state
      ->setErrorByName('cookie_domain', $this
      ->t("The cookie domain seems to be invalid."));
  }

  // Validate hash value may not contain the #-char.
  if (($value = $form_state
    ->getValue('open_settings_hash')) && !(strpos($value, '#') === FALSE)) {
    $form_state
      ->setErrorByName('open_settings_hash', $this
      ->t("The hash value may not contain the #-char."));
  }

  // Validate hash value has only valid chars.
  if (($value = $form_state
    ->getValue('open_settings_hash')) && preg_match('/^[a-zA-Z0-9_\\-]{3,64}$/', $value) !== 1) {
    $form_state
      ->setErrorByName('open_settings_hash', $this
      ->t("The hash value is invalid. Permitted are between 3 and 64 characters of (0-9 a-z A-Z _ -)."));
  }
  parent::validateForm($form, $form_state);
}