ValidPathConstraintValidator.php in Drupal 8
File
core/lib/Drupal/Core/Path/Plugin/Validation/Constraint/ValidPathConstraintValidator.php
View source
<?php
namespace Drupal\Core\Path\Plugin\Validation\Constraint;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Path\PathValidatorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class ValidPathConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
protected $pathValidator;
public function __construct(PathValidatorInterface $path_validator) {
$this->pathValidator = $path_validator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('path.validator'));
}
public function validate($value, Constraint $constraint) {
if (!isset($value)) {
return;
}
$path = trim($value, '/');
if (!$this->pathValidator
->isValid($path)) {
$this->context
->addViolation($constraint->message, [
'%link_path' => $value,
]);
}
}
}