You are here

public function DirectoryForm::validateForm in Private files download permission 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/DirectoryForm.php \Drupal\pfdp\Form\DirectoryForm::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/DirectoryForm.php, line 83

Class

DirectoryForm
Directory form for private_files_download_permission.

Namespace

Drupal\pfdp\Form

Code

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

  // Retrieve $path (which, being required, is surely not blank).
  $path = $form_state
    ->getValue('path');

  // Perform slash validation:
  if (0 < mb_strlen($path)) {
    $first_character = mb_substr($path, 0, 1);
    $last_character = mb_substr($path, -1, 1);

    // ...there must be a leading slash.
    if ('/' !== $first_character && '\\' !== $first_character) {
      $form_state
        ->setErrorByName('path', $this
        ->t('You must add a leading slash.'));
    }
    if (1 < mb_strlen($path)) {

      // ...there cannot be multiple consecutive slashes.
      if (FALSE !== mb_strpos($path, '//') || FALSE !== mb_strpos($path, '\\\\')) {
        $form_state
          ->setErrorByName('path', $this
          ->t('You cannot use multiple consecutive slashes.'));
      }

      // ...there cannot be trailing slashes.
      if ('/' === $last_character || '\\' === $last_character) {
        $form_state
          ->setErrorByName('path', $this
          ->t('You cannot use trailing slashes.'));
      }
    }
  }
}