-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add AutoMapping constraint to enable or disable auto-validation #32107
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
Conversation
2113beb
to
de30553
Compare
Hi @dunglas, this means in sf 4.3 it is a default feature (doctrine field are alwas by default validated) ? |
@noniagriconomie see symfony/recipes#612 BTW, I think we also need an opt-in annotation |
I agree with Nicolas here, Opt-In is definitely needed too 👍🏻 |
thx for both answers @nicolas-grekas @OskarStark :) |
src/Symfony/Component/Validator/Mapping/Loader/AutoMappingTrait.php
Outdated
Show resolved
Hide resolved
IMHO it should be enabled by default in the recipes (and allow to opt-out some classes or properties) and having an opt-in feature is a nice to have. |
As we experienced issues when we enabled it by default and side effects that are difficult to understand, I'm in favor of only supporting the opt-in mode and only for classes/properties. |
Being opt-in only defects the purpose of this feature and decreases the security benefits it provides: #32070 (comment) Most side effects are because this feature is young, this PR should be enough to prevent the last remaining ones. The feature is already opt-in using the config file (it’s off by default). I don’t understand how the AutoMapping constraint would work? I mean, you must already opt in a specific directory using the config, and you can exclude some classes or properties using this new annotation. How the proposed annotation would integrate with this? |
src/Symfony/Bridge/Doctrine/Tests/Validator/DoctrineLoaderTest.php
Outdated
Show resolved
Hide resolved
Opt-in can still be made the default in maker-bundle, so that the security you want @dunglas is still a thing. |
Many people (including me) don't use maker bundle to create entities (for instance, almost all PHPStorm users, I guess). And it's one more annotation to not forget to add to every new class, for no reasons. Actually, I prefer the current situation (having to uncomment one time for all some lines in a config file) than having to add an annotation to all new entities. |
Travis failures are expected (we'll have to bump the required Validator version or to add conflict rules when merged). |
That's why I think we need config + annotation, opt-in/out + list/exclusion, to cover all styles. |
Here is a new proposal that should satisfy everybody:
It should cover all use cases, the feature is still on by default for the users of the framework thanks to the recipe. Do you folks agree? |
Annotations always win, looks sensible to me. |
Should target 4.4 to me, except the bug fix part of course. |
With this proposal, if no annotations are added (assuming we re-add the auto mapping config in the recipe in 4.4), then auto mapping would still be turned on, correct? We could then, in make:entity decide to always generate entities with AutoMapping(true) to be explicit and make it more obvious how to turn it off (or even answer the question “where is this validation coming from?”). I think that sensible... though we may debate later whether to have the default config be “always on without annotation” or “off, unless the annotation is present” (or an I misunderstanding how the config would work?) Thanks for the conversation. This feature makes me a bit uncomfortable... but @dunglas may very well be correct that it’s just this that’s needed to make it shine. After all, the form fields themselves hav built-in, automatic “sanity” validation, and that’s never been a problem (not an exact comparison). |
I think disabling all automatic constraints at once is a bit rough. Think, for example, of a decimal property type-hinted to float (or a Decimal object or whatever) instead of Doctrine's internal type string. At the moment, this leads to an invalid Wouldn't it be less invasive to only disable the specifically problematic automatic constraints? Like |
4c589c5
to
5059899
Compare
4104e22
to
1376105
Compare
This one should be ready to be merged now. The last decision to make is between this single annotation and |
I think I prefer Enable/DisableAutoMapping because it opens for better extensibility.
Then |
065a81c
to
a1764e8
Compare
|
a1764e8
to
f6519ce
Compare
Thank you Kévin. |
As discussed in #32070 and #32015, it's sometimes mandatory to prevent some classes or properties to be auto mapped (auto-validated). This PR introduces a new constraint,
@AutoMapping
allowing to do exactly that. Examples:Class:
Property:
The rules are the following: