class ConstraintValidatorFactory

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Validation/ConstraintValidatorFactory.php \Drupal\Core\Validation\ConstraintValidatorFactory
  2. 10 core/lib/Drupal/Core/Validation/ConstraintValidatorFactory.php \Drupal\Core\Validation\ConstraintValidatorFactory
  3. 11.x core/lib/Drupal/Core/Validation/ConstraintValidatorFactory.php \Drupal\Core\Validation\ConstraintValidatorFactory

Defines a constraint validator factory that works with container injection.

@TODO Decide what to do with this class or how to reuse constraint validators in https://drupal.org/project/drupal/issues/3097071

Hierarchy

Expanded class hierarchy of ConstraintValidatorFactory

1 file declares its use of ConstraintValidatorFactory
TypedDataManager.php in core/lib/Drupal/Core/TypedData/TypedDataManager.php

File

core/lib/Drupal/Core/Validation/ConstraintValidatorFactory.php, line 16

Namespace

Drupal\Core\Validation
View source
class ConstraintValidatorFactory extends BaseConstraintValidatorFactory {
    
    /**
     * The class resolver.
     */
    protected $classResolver;
    
    /**
     * Constructs a new ConstraintValidatorFactory.
     *
     * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
     *   The class resolver.
     */
    public function __construct(ClassResolverInterface $class_resolver) {
        $this->classResolver = $class_resolver;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getInstance(Constraint $constraint) : ConstraintValidatorInterface {
        $class_name = $constraint->validatedBy();
        // Constraint validator instances should always be initialized newly and
        // never shared, because the current validation context is getting injected
        // into them through setter injection and in a case of a recursive
        // validation where a validator triggers a validation chain leading to the
        // same validator the context of the first call would be exchanged with the
        // one of the subsequent validation chain.
        return $this->classResolver
            ->getInstanceFromDefinition($class_name);
    }

}

Members

Title Sort descending Modifiers Object type Summary
ConstraintValidatorFactory::$classResolver protected property The class resolver.
ConstraintValidatorFactory::getInstance public function
ConstraintValidatorFactory::__construct public function Constructs a new ConstraintValidatorFactory.

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