You are here

class ValidPathConstraintValidator in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Path/Plugin/Validation/Constraint/ValidPathConstraintValidator.php \Drupal\Core\Path\Plugin\Validation\Constraint\ValidPathConstraintValidator

Constraint validator for validating system paths.

Hierarchy

Expanded class hierarchy of ValidPathConstraintValidator

File

core/lib/Drupal/Core/Path/Plugin/Validation/Constraint/ValidPathConstraintValidator.php, line 14

Namespace

Drupal\Core\Path\Plugin\Validation\Constraint
View source
class ValidPathConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {

  /**
   * The path validator.
   *
   * @var \Drupal\Core\Path\PathValidatorInterface
   */
  protected $pathValidator;

  /**
   * Creates a new ValidPathConstraintValidator instance.
   *
   * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
   *   The path validator.
   */
  public function __construct(PathValidatorInterface $path_validator) {
    $this->pathValidator = $path_validator;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('path.validator'));
  }

  /**
   * {@inheritdoc}
   */
  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,
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ValidPathConstraintValidator::$pathValidator protected property The path validator.
ValidPathConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ValidPathConstraintValidator::validate public function Checks if the passed value is valid.
ValidPathConstraintValidator::__construct public function Creates a new ValidPathConstraintValidator instance.