ValidPathConstraintValidator.php

Same filename and directory in other branches
  1. 9 core/lib/Drupal/Core/Path/Plugin/Validation/Constraint/ValidPathConstraintValidator.php
  2. 8.9.x core/lib/Drupal/Core/Path/Plugin/Validation/Constraint/ValidPathConstraintValidator.php
  3. 10 core/lib/Drupal/Core/Path/Plugin/Validation/Constraint/ValidPathConstraintValidator.php

Namespace

Drupal\Core\Path\Plugin\Validation\Constraint

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;

/**
 * Constraint validator for validating system paths.
 */
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) : void {
        if (!isset($value)) {
            return;
        }
        $path = trim($value, '/');
        if (!$this->pathValidator
            ->isValid($path)) {
            $this->context
                ->addViolation($constraint->message, [
                '%link_path' => $value,
            ]);
        }
    }

}

Classes

Title Deprecated Summary
ValidPathConstraintValidator Constraint validator for validating system paths.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.