You are here

public function Search404Settings::validateForm in Search 404 8

Same name and namespace in other branches
  1. 2.x src/Form/Search404Settings.php \Drupal\search404\Form\Search404Settings::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/Form/Search404Settings.php, line 207

Class

Search404Settings
Configure settings for search404.

Namespace

Drupal\search404\Form

Code

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

  // Validation for redirect url.
  if (!empty($form_state
    ->getValue('search404_page_redirect'))) {
    $path = $form_state
      ->getValue('search404_page_redirect');
    if (strpos($path, ' ') === 0) {
      $form_state
        ->setErrorByName('search404_page_redirect', $this
        ->t('Invalid url : Redirect url should not be a space or not start with a space.'));
    }
    if (strpos($path, '/') !== 0) {
      $form_state
        ->setErrorByName('search404_page_redirect', $this
        ->t('Invalid url : Redirect url should be start with a slash.'));
    }
  }

  // Validation for custom search path.
  if (!empty($form_state
    ->getValue('search404_do_custom_search'))) {
    $custom_path = $form_state
      ->getValue('search404_custom_search_path');
    if (empty(preg_match("/\\/@keys\$/", $custom_path))) {
      $form_state
        ->setErrorByName('search404_custom_search_path', $this
        ->t('Custom search path should be ends with search key pattern "/@keys".'));
    }
    $url_path = explode("@keys", $custom_path);
    if (!empty(preg_match('/[\'^£!`$%&*()\\{}\\:.;,\\[\\]"@#~?><>,|=_+¬-]/', $url_path[0]))) {
      $form_state
        ->setErrorByName('search404_custom_search_path', t('Custom search path should not contains special characters other than "/"'));
    }
    if (strpos($custom_path, '/') === 0) {
      $form_state
        ->setErrorByName('search404_custom_search_path', $this
        ->t('Custom search path should not be start with a slash.'));
    }
  }
}