You are here

public function UrlInternalExternal::addValidation in Clientside Validation 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/CvValidator/UrlInternalExternal.php \Drupal\clientside_validation\Plugin\CvValidator\UrlInternalExternal::addValidation()
  2. 3.0.x src/Plugin/CvValidator/UrlInternalExternal.php \Drupal\clientside_validation\Plugin\CvValidator\UrlInternalExternal::addValidation()
  3. 2.0.x src/Plugin/CvValidator/UrlInternalExternal.php \Drupal\clientside_validation\Plugin\CvValidator\UrlInternalExternal::addValidation()

Makes the necessary changes to the form element so it can be validated.

Parameters

array $element: The form element to validate.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the form this element belongs to.

Overrides CvValidatorBase::addValidation

File

src/Plugin/CvValidator/UrlInternalExternal.php, line 43

Class

UrlInternalExternal
Provides a 'url' validator for internal url.

Namespace

Drupal\clientside_validation\Plugin\CvValidator

Code

public function addValidation(array &$element, FormStateInterface $form_state) {
  parent::addValidation($element, $form_state);

  // Needs patch from https://www.drupal.org/node/2613694
  switch ($element['#link_type']) {
    case LinkItemInterface::LINK_GENERIC:
      $element['#attributes']['pattern'] = '\\<front\\>|\\/.*|\\?.*|#.*|[hH][tT][Tt][pP][sS]?://.+|.*\\(\\d+\\)';
      break;
    case LinkItemInterface::LINK_INTERNAL:
      $element['#attributes']['pattern'] = '\\<front\\>|\\/|\\/[^\\/]+.*|\\?.*|#.*|.*\\(\\d+\\)';
      break;
  }
}