You are right, this tag (validator.constraint_validator
) processed by compiler-pass from Symfony/FrameworkBundle
.
If you don't want to use this bundle, but you want to link DI container with validator, you should re-implement this process.
First, we need an instance of ConstraintValidatorFactoryInterface. Default implementation just looks for a class, with name returned by validatedBy() method of constraint. Implementation from FrameworkBundle considers result of validatedBy() as service identifier/or tag alias and gets this service from container. Our implementation should do something like that:
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\ConstraintValidatorFactoryInterface;
use Symfony\Component\Validator\Constraint;
class ContainerConstraintValidatorFactory implements ConstraintValidatorFactoryInterface
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getInstance(Constraint $constraint)
{
// Simplified implementation, just for example:
return $this->container->get($constraint->validatedBy());
}
}
For example, suppose we have some custom validation constraint and validator class with some dependency:
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CustomConstraint extends Constraint
{
public $message = 'Oops!';
public function validatedBy()
{
return 'validator.custom';
}
}
class CustomConstraintValidator extends ConstraintValidator
{
private $dependency;
public function __construct($dependency)
{
$this->dependency = $dependency;
}
public function validate($value, Constraint $constraint)
{
if (rand() % 2 === 0) {
$this->context
->buildViolation($constraint->message)
->addViolation();
}
}
}
Next we register constraint validator as service with ID same as result of validatedBy()
method from related constraint object. We create instance of the validator service related with container through this factory. All constraints validators, registered as same manner will be available.
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Validator\Validation;
$container = new ContainerBuilder();
$container->register(
'validator.custom',
new CustomConstraintValidator('Some dependency value')
);
$validatorBuilder = Validation::createValidatorBuilder();
$validatorBuilder->setConstraintValidatorFactory(
new ContainerConstraintValidatorFactory($container)
);
$validator = $validatorBuilder->getValidator();
$violations = $validator->validate('test value', [
new CustomConstraint()
]);
It works, however I recommend to consider of using FrameworkBundle
to not reinvent the wheel. You can configure it to use only things that you need.