Description
Sometimes (for advanced cases), it is useful to execute validation of other constraints in the implementation of a ConstraintValidator.
The supported way of doing that in symfony/validator
is to use $this->context->getValidator()
(following the usage described in https://github.com/symfony/symfony/blob/v7.3.0/src/Symfony/Component/Validator/Context/ExecutionContextInterface.php#L21-L58).
When providing support on the Symfony Slack, I encountered a case where a dev has injected the ValidatorInterface
in the constraint instead and uses that. This broke everything because the main validator (entrypoint of the component) is injecting its current execution context in all the registered constraint validator, which will impact the in-progress validation when starting a new one (constraint validators will not attach all their violations to the new execution context even when not expected).
A static analysis rule could prevent such mistake by detecting places where we inject the Symfony\Component\Validator\Validator\ValidatorInterface
(either through the constructor or through a method that has the #[Required]
attribute of symfony/service-contracts
) in a class implementing Symfony\Component\Validator\ConstraintValidatorInterface
.