Skip to content

ConstraintValidator and ConstraintValidatorInterface use deprecated entity #11049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tobias-93 opened this issue Jun 3, 2014 · 6 comments
Closed

Comments

@tobias-93
Copy link
Contributor

tobias-93 commented Jun 3, 2014

The parameter $context of Symfony\Component\Validator\ConstraintValidator and ConstraintValidatorInterface is of type Symfony\Component\Validator\ExecutionContextInterface, which is deprecated. It would be useful for projects using the Validator api which want to update to the new api, to allow using the new entity Symfony\Component\Validator\Context\ExecutionContextInterface, since it adds the replacement buildViolation for the deprecated addViolationAt function.
Could you add that support please?

Thanks, Tobias

@stof
Copy link
Member

stof commented Jun 4, 2014

@webmozart any idea how to solve this ?

@tobias-93
Copy link
Contributor Author

@stof @webmozart Any update on this yet?

@webmozart
Copy link
Contributor

@tobias-93 The solution is to (a) configure your project to use the 2.5 API and (b) simply use the methods of the new interface. In order to circumvent IDE errors, you can add:

use Symfony\Component\Validator\Context\ExecutionContextInterface;

class MyValidator extends ConstraintValidator
{
    /**
     * @var ExecutionContextInterface
     */
    protected $context;

    // ...
}

to your constraint validator (or your project's base validator). Be aware that if you use the new methods, your code cannot be used with the <2.5 API anymore (obviously). So my recommendation is:

  • use the new interface for 2.5+ projects/bundles
  • use the old interface for bundles that should be compatible with <2.5

@stof
Copy link
Member

stof commented Jul 17, 2014

@webmozart are all core constraint validators written in a way support all APIs (i.e 2.4, 2.5-bc and 2.5) ? Or do they require enabling at least the legacy mode ?
same for bundles needing to continue to support the 2.3 LTS. Can they be compatible with 2.5 without legacy mode easily, or does it require big changes ?

@webmozart
Copy link
Contributor

@stof Currently, both the 2.5-bc and 2.5 APIs use the class LegacyExecutionContext. In this way the old validators can still use the old context API even with the 2.5 API.

However, I guess it would be wise to change this to use ExecutionContext in the 2.5 API so that our users can check whether they use the old API by just making the switch and checking for errors. We could fix the core validators by adding:

  • LegacyAllValidator (and likewise for all validators that use the old API - shouldn't be many)
  • LegacyConstraintValidatorFactory (to use the legacy validators)

@tobias-93
Copy link
Contributor Author

@webmozart Thank you for your comments, I will adapt my project using your guideline.

webmozart added a commit that referenced this issue Aug 4, 2014
…ebmozart)

This PR was squashed before being merged into the 2.5 branch (closes #11485).

Discussion
----------

[Validator] Constraint validators now use the 2.5 API

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

See the comments in #11049 for the origin of this PR.

Currently, the 2.5 API needs to use `LegacyExecutionContextFactory` because the constraint validators rely on methods from the old `ExecutionContext` class (like `validate()`, `validateValue()`). Consequently it is impossible to switch to the pure 2.5 API and check whether all calls to deprecated methods were removed from application code. This is fixed now.

This PR also introduces a complete test suite to test each constraint validator against all three APIs: 2.4, 2.5-BC and 2.5. Currently, some tests are not executed yet when running the complete test suite is run. I expect this to be fixed soon (ticket: sebastianbergmann/phpunit#529, pr: sebastianbergmann/phpunit#1327).

Commits
-------

295e5bb [Validator] Fixed failing tests
3bd6d80 [Validator] CS fixes
870a41a [FrameworkBundle] Made ConstraintValidatorFactory aware of the legacy validators
7504448 [Validator] Added extensive test coverage for the constraint validators for the different APIs
8e461af [Validator] Constraint validators now use the 2.5 API. For incompatible validators, legacy validators were created
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants