Skip to content

[Rule idea] Prevent injecting the ValidatorInterface in classes extending ConstraintValidator #444

Description

@stof

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions