-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Validation Constraint Inheritance Broken #21567
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
Comments
ping @uwej711 |
Just noticed, moving this piece of code // Include constraints from the parent class
$this->mergeConstraints($metadata); from line 123 to line 115 right after the |
Rolling back the changes from #21053 fixes it for me. |
So it seems we have conflicting requirements here and in #15950 which are more or less mutually exclusive. I am actually not sure how to solve this sufficiently. Maybe we can mitigate #15950 by modifying the patch from #21053 partially by skipping the merge only when constraints being used are the same (regardless their options)? What do you think @symfony/deciders? |
Imo. a sub class can only add additional constraints, at least by default that is. You're extending by definition ( That said.. what about |
Agree with @ro0NL that by default constraints should extend constraints from super-classes instead of overriding them. In my opinion, it should only override property constraints, when the related property has been overwritten in the sub-class. So the decision, which constraints should be applied, can only be made if we take into account the class hierarchy and how the properties of these classes are defined in addition to the constraint metadata. To make clear what I mean, let's say we have constraints defined on both "Extends" case: class Foo {
public $attribute;
}
class Bar extends Foo {
}
"Override" case: class Foo {
public $attribute;
}
class Bar extends Foo {
public $attribute;
}
Overall it looks to me that the MetadataFactory needs to become aware of how the classes look like, it cannot just merge constraint metadata the one or the other way. |
@scheb that's a way of looking at it :) but im not sure it's convenient to mix&match the PHP with the XML definition or so. Ie. adding the attribute in your "override" example changes the behavior of the XML definition.. that's weird. For annotations it could work, as you need to define the property anyway to annotate it with constraints.. but imo. it's highly implicit behavior (ie. magic). Basically we need a fix for #15950 only. |
…sses (angelk, xabbuh) This PR was merged into the 2.7 branch. Discussion ---------- [Validator] property constraints can be added in child classes | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21538, #21567 | License | MIT | Doc PR | This reverts the changes done in #21053 (and applies the test written by @angelk in #21538). I think trying to "fix" #15950 with the changes from #21053 was a mistake. Child classes should be able to refine validation constraints (I basically agree with @ro0NL's reasoning in #21567 (comment). Commits ------- 9513a8a property constraints can be added in child classes a266ff7 added test for staticClassLoader in LazyLoadingMetadatafactory
I have proposed |
Uh oh!
There was an error while loading. Please reload this page.
Symfony validator does no longer inherit all constraints from the parent class. It seems like all the constraints from super-classes are lost when there are additional constraints on the same attribute in the sub-class. I was able to track it down to this commit, if I roll-back the changes it's working again. But couldn't wrap my head around yes, why this is happening.
Test script to reproduce the bug:
The text was updated successfully, but these errors were encountered: