UrlInternalExternal.php in Clientside Validation 2.0.x
File
src/Plugin/CvValidator/UrlInternalExternal.php
View source
<?php
namespace Drupal\clientside_validation\Plugin\CvValidator;
use Drupal\clientside_validation\CvValidatorBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\link\LinkItemInterface;
class UrlInternalExternal extends CvValidatorBase {
protected function getRules($element, FormStateInterface $form_state) {
$message = $element['#url_internal_external_error'] ?? $this
->t('@title does not contain a valid url.', [
'@title' => $this
->getElementTitle($element),
]);
return [
'messages' => [
'pattern' => $message,
],
];
}
public function addValidation(array &$element, FormStateInterface $form_state) {
parent::addValidation($element, $form_state);
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;
}
}
}