You are here

public function CookiesTextsForm::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/CookiesTextsForm.php, line 251

Class

CookiesTextsForm
Configure site information settings for this site.

Namespace

Drupal\cookies\Form

Code

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

  // Validate front page path.
  if (($value = $form_state
    ->getValue('imprintUri')) && !preg_match('/^http(s)?:\\/\\//', $value) && $value[0] !== '/') {
    $form_state
      ->setErrorByName('imprintUri', $this
      ->t("The path '%path' has to start with a slash.", [
      '%path' => $form_state
        ->getValue('imprintUri'),
    ]));
  }
  if (!preg_match('/^http(s)?:\\/\\//', $value) && !$this->pathValidator
    ->isValid($form_state
    ->getValue('imprintUri'))) {
    $form_state
      ->setErrorByName('imprintUri', $this
      ->t("Either the path '%path' is invalid or you do not have access to it.", [
      '%path' => $form_state
        ->getValue('imprintUri'),
    ]));
  }

  // Get the normal paths of both error pages.
  if (!$form_state
    ->isValueEmpty('imprintUri')) {
    $form_state
      ->setValueForElement($form['links']['imprintUri'], $this->aliasManager
      ->getPathByAlias($form_state
      ->getValue('imprintUri')));
  }

  // Validate privacy uri.
  if (($value = $form_state
    ->getValue('privacyUri')) && !preg_match('/^http(s)?:\\/\\//', $value) && $value[0] !== '/') {
    $form_state
      ->setErrorByName('privacyUri', $this
      ->t("The path '%path' has to start with a slash.", [
      '%path' => $form_state
        ->getValue('privacyUri'),
    ]));
  }
  if (!preg_match('/^http(s)?:\\/\\//', $value) && !$this->pathValidator
    ->isValid($form_state
    ->getValue('privacyUri'))) {
    $form_state
      ->setErrorByName('privacyUri', $this
      ->t("Either the path '%path' is invalid or you do not have access to it.", [
      '%path' => $form_state
        ->getValue('privacyUri'),
    ]));
  }

  // Get the normal paths of both error pages.
  if (!$form_state
    ->isValueEmpty('privacyUri')) {
    $form_state
      ->setValueForElement($form['links']['privacyUri'], $this->aliasManager
      ->getPathByAlias($form_state
      ->getValue('privacyUri')));
  }

  // Validate front page path.
  if (($value = $form_state
    ->getValue('cookieDocsUri')) && !preg_match('/^http(s)?:\\/\\//', $value) && $value[0] !== '/') {
    $form_state
      ->setErrorByName('cookieDocsUri', $this
      ->t("The path '%path' has to start with a slash.", [
      '%path' => $form_state
        ->getValue('cookieDocsUri'),
    ]));
  }
  if (!preg_match('/^http(s)?:\\/\\//', $value) && !$this->pathValidator
    ->isValid($form_state
    ->getValue('cookieDocsUri'))) {
    $form_state
      ->setErrorByName('cookieDocsUri', $this
      ->t("Either the path '%path' is invalid or you do not have access to it.", [
      '%path' => $form_state
        ->getValue('cookieDocsUri'),
    ]));
  }

  // Get the normal paths of both error pages.
  if (!$form_state
    ->isValueEmpty('cookieDocsUri')) {
    $form_state
      ->setValueForElement($form['links']['cookieDocsUri'], $this->aliasManager
      ->getPathByAlias($form_state
      ->getValue('cookieDocsUri')));
  }
  parent::validateForm($form, $form_state);
}