-
-
Notifications
You must be signed in to change notification settings - Fork 955
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
Partial update and constraint validations #1637
Comments
In a project i'm working we handled it this way: We created a class that creates groups for every attribute send in the request:
And then you can add the generated groups to the validator:
But you still have to add every constraint to the expected validation group.
|
EDIT: I had a similar problem with the DateTime constraint. ============================================================================= The @GHuygen solution works, but this not a solution, this is DIY. Patch requests are supposed to ignore unsent fields, and therefore not trigger their validation. |
The Symfony Validators on one field also have access to the values of all other fields. This is necessary for some stock validations (e.g. So in the general case, validations don't just concern one field, and therefore we can't automatically do any better than running all validations even on partial PATCH. After all, changing just the "currency" field may make the "amount" field invalid implicitly. At least not as long as Symfony doesn't introduce something like "pure validations" which don't have access to any other fields, or any kind of dependency tracking for validations. Personally I embrace the current status, because it gives me stronger guarantees that the objects saved into the database are always valid, and because the validation groups give me enough flexibility for any use case I have come across so far. |
I agree with @carlobeltrame , the Symfony validation system ensures the validity of your whole entity, not only your request. I can't see any business logic where you would accept to patch / update a non-valid entity. I would just think about performances, but it's a small effort. |
I have met the similar situation, this also applies when creating/patching entity and e.g. (in some scenarios) selected fields are filled otherwise, like set user/owner/email etc. from logged in user. The workaround could be to turn off the validation on the API deserialization step (
and format validation's result in |
Hi,
I have an entity with many fields, and I have to allow a partial update (PATCH action) via API Platform of some fields.
Depending on front interface, any field of this entity will be updated or not.
I added some constraint on these fields, via Assert annotations.
Problem is when I execute my patch action, all constraints validation are executed, even if field is not present in my request.
Is there a solution to ignore constraints for non defined fields?
I would like to avoid create many validation groups, because it creates a constraint relation between front office/ back office.
Thanks for your help.
The text was updated successfully, but these errors were encountered: