Symfony validator inheritance Symfony validator inheritance symfony symfony

Symfony validator inheritance


I am going to conclude my search for the solution for now. It is clear that there was an attempt to resolve this but it caused some sort of BC breakage, however I have not found any mentions of it (so we could better understand it). After all, major versions allow BC breaks and, in my opinion, either 3.0 or 4.0 were a good candidates for this, but somehow slipped thought.

The best lead I've got (but not sufficient nor ideal in any means) is the quote from docs:

This means you are able to add new constraints to a property, but you cannot override them ... To create your own validation, add the constraints to a new validation group

Indeed, defining own own validation groups could be a potential solution to this, but we are back to square one if we do not have a control over parent classes (and we usually don't). So, this would involve redefining all of the properties from base class in our child class. This really beats the purpose.

Derived on above, somewhat less funky solution can be applied, if we externalize our validation rules to YAML or XML. So, no annotations. In YAML, that would look something like this:

App\Data\Foo:    properties:        attempts:            - LessThanOrEqual: 2        another:            - LessThanOrEqual: 5App\Data\Bar:    properties:        attempts:            - LessThanOrEqual: {value: 5, groups: [Overriden]}        another:            - LessThanOrEqual: {value: 5, groups: [Overriden]}

In the config example above, Bar extends Foo and we can copy and paste existing validation rules and stick the custom group (Overriden in my case) and use it. That way, we validate those constraints coming from parent class, that are of interest to us and include our own from child class. Slightly more effort is needed if parent class uses different validation rule format...

I am eager to see if anyone can come up with better aproach as I feel this should really work our-of-box...

Hope this helps a bit...