You are here

public function ProtectedPagesEditForm::validateForm in Protected Pages 8

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/ProtectedPagesEditForm.php, line 145

Class

ProtectedPagesEditForm
Provides an edit protected page form.

Namespace

Drupal\protected_pages\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $entered_path = rtrim(trim($form_state
    ->getValue('path')), " \\/");
  if (substr($entered_path, 0, 1) != '/') {
    $form_state
      ->setErrorByName('path', $this
      ->t('The path needs to start with a slash.'));
  }
  else {
    $normal_path = $this->aliasManager
      ->getPathByAlias($form_state
      ->getValue('path'));
    $path_alias = mb_strtolower($this->aliasManager
      ->getAliasByPath($form_state
      ->getValue('path')));
    if (!$this->pathValidator
      ->isValid($normal_path)) {
      $form_state
        ->setErrorByName('path', $this
        ->t('Please enter a correct path!'));
    }
    $fields = [
      'pid',
    ];
    $conditions = [];
    $conditions['or'][] = [
      'field' => 'path',
      'value' => $normal_path,
      'operator' => '=',
    ];
    $conditions['or'][] = [
      'field' => 'path',
      'value' => $path_alias,
      'operator' => '=',
    ];
    $conditions['and'][] = [
      'field' => 'pid',
      'value' => $form_state
        ->getValue('pid'),
      'operator' => '<>',
    ];
    $pid = $this->protectedPagesStorage
      ->loadProtectedPage($fields, $conditions, TRUE);
    if ($pid) {
      $form_state
        ->setErrorByName('path', $this
        ->t('Duplicate path entry is not allowed. There is already a path or its alias exists.'));
    }
  }
}