You are here

public function ModalForm::validateForm in Modal 5.0.x

Same name and namespace in other branches
  1. 8.3 src/Form/ModalForm.php \Drupal\modal_page\Form\ModalForm::validateForm()
  2. 8.2 src/Form/ModalForm.php \Drupal\modal_page\Form\ModalForm::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/ModalForm.php, line 343

Class

ModalForm
Class ModalForm.

Namespace

Drupal\modal_page\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();
  if (empty($values['pages'])) {
    return FALSE;
  }
  $pages = $values['pages'];
  $urlList = [];
  $urlList = explode(PHP_EOL, $pages);
  foreach ($urlList as $url) {
    $trimUrl = trim($url);

    // Validate Slash.
    if ($trimUrl !== '<front>' && $trimUrl[0] !== '/' && $trimUrl[0] !== '') {
      $form_state
        ->setErrorByName('pages', $this
        ->t("@url path needs to start with a slash.", [
        '@url' => $trimUrl,
      ]));
    }

    // Validate wildcard.
    if (strpos($trimUrl, '*') !== FALSE && substr($trimUrl, -1) != '*') {
      $form_state
        ->setErrorByName('pages', $this
        ->t("The wildcard * must be used at the end of the path. E.g. /admin/*"));
    }
  }
}