You are here

public function PathFormBase::validateForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/path/src/Form/PathFormBase.php \Drupal\path\Form\PathFormBase::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

core/modules/path/src/Form/PathFormBase.php, line 162
Contains \Drupal\path\Form\PathFormBase.

Class

PathFormBase
Provides a base class for path add/edit forms.

Namespace

Drupal\path\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $source =& $form_state
    ->getValue('source');
  $source = $this->aliasManager
    ->getPathByAlias($source);
  $alias =& $form_state
    ->getValue('alias');

  // Trim the submitted value of whitespace and slashes. Ensure to not trim
  // the slash on the left side.
  $alias = rtrim(trim(trim($alias), ''), "\\/");
  if ($source[0] !== '/') {
    $form_state
      ->setErrorByName('source', 'The source path has to start with a slash.');
  }
  if ($alias[0] !== '/') {
    $form_state
      ->setErrorByName('alias', 'The alias path has to start with a slash.');
  }

  // Language is only set if language.module is enabled, otherwise save for all
  // languages.
  $langcode = $form_state
    ->getValue('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED);
  if ($this->aliasStorage
    ->aliasExists($alias, $langcode, $this->path['source'])) {
    $stored_alias = $this->aliasStorage
      ->load([
      'alias' => $alias,
      'langcode' => $langcode,
    ]);
    if ($stored_alias['alias'] !== $alias) {

      // The alias already exists with different capitalization as the default
      // implementation of AliasStorageInterface::aliasExists is
      // case-insensitive.
      $form_state
        ->setErrorByName('alias', t('The alias %alias could not be added because it is already in use in this language with different capitalization: %stored_alias.', [
        '%alias' => $alias,
        '%stored_alias' => $stored_alias['alias'],
      ]));
    }
    else {
      $form_state
        ->setErrorByName('alias', t('The alias %alias is already in use in this language.', [
        '%alias' => $alias,
      ]));
    }
  }
  if (!$this->pathValidator
    ->isValid(trim($source, '/'))) {
    $form_state
      ->setErrorByName('source', t("The path '@link_path' is either invalid or you do not have access to it.", array(
      '@link_path' => $source,
    )));
  }
}