You are here

public function Search404Settings::validateForm in Search 404 2.x

Same name and namespace in other branches
  1. 8 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 216

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('The redirect URL should not be a space, and should not start with a space.'));
    }
    if (strpos($path, '/') !== 0) {
      $form_state
        ->setErrorByName('search404_page_redirect', $this
        ->t('The redirect URL should 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 end with the following text: @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 contain special characters other than "/", "=", "?" and "&".'));
    }
    if (strpos($custom_path, '/') === 0) {
      $form_state
        ->setErrorByName('search404_custom_search_path', $this
        ->t('Custom search path should not start with a slash.'));
    }
  }
}