Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[Validator] The validator does not support inheritance properly for getter rules #2841

Closed
stof opened this Issue · 8 comments

7 participants

Christophe Coevoet Kris Wallsmith Johannes abretaud Kasheen Wouter J Jakub Zalas
Christophe Coevoet
Collaborator

The Validator component uses Reflection, even when doing the validation on getters. This means that a child class cannot overwrite a getter as the validator will still use the parent method.
this is totally confusing (I just spent quite 2 hours debugging my form validation before thinking about Reflection being used) and does not make sense IMO (when overwriting a method, we expect the child one to be called)
what do you think about it ?

Kris Wallsmith

I agree that using reflection for methods is problematic. I have tried to define constraints on an interface before, which doesn't work at all. You get this error…

PHP Warning: Uncaught exception 'ReflectionException' with message 'Trying to invoke abstract method FooInterface::foo()'

Johannes

I think there are two cases which need to be distinguished:

  1. Using annotations:
    In this case, I expect validation to happen wherever I specify the annotation (property, or method). If a method with an annotation gets overridden, then the constraint should be ignored imo.

  2. Using other metadata sources:
    In this case, it's probably more intuitive to validate the outermost method.

As a side-note, the metadata library provides a very good toolset to handle such cases (including metadata on interfaces as supported by JMSSecurityExtraBundle).

Kris Wallsmith

Why should annotations behave differently than other drivers?

Christophe Coevoet stof referenced this issue from a commit in FriendsOfSymfony/FOSUserBundle
Christophe Coevoet stof Added a validation initializer for Propel proxies
If symfony/symfony#2841 is fixed, we will be able to use getter
constraints instead of having to update the parent properties
66de3d2
Johannes

@kriswallsmith, I guess it would make sense to use the annotation behavior for the other drivers as well (that is what I'm doing in my bundles at least).

abretaud

I don't know if it's really related, but if you want to overwrite a Choice constraint callback function, you end up with the same behavior i.e. the callback is called on the class where the annotation is written. And as you can't overwrite an annotation, there's no good solution.
I would really like to have better inheritance support for annotations in validator.

Kasheen

I've run into the same issue as @kriswallsmith was having with regards to trying to annotate interface getter methods.

Seems like it would be something useful to support since it means you don't need to copy your validation constraints to all classes that implement the contract of the interface.

It seems intuitive to me that validation constraints on a parent class are inherited onto a child class unless overridden (by which I mean different constraints are annotated) - perhaps for those who want to add to the parent constraints there could be something like @Assert\Parent followed by any further constraints that need to be checked (or if overriding a method removed constraints by default then at least an @Assert\Parent would let you put them back easily without copy-pasting).

Christophe Coevoet
Collaborator

@bschussek ping

Wouter J

This is fixed by #7271 and can be closed

Jakub Zalas jakzal closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.