I think that which field I am validating is not a matter in this situtation. It is completely clear that I am validating the field which I put annotation on. For example, consider situation where I want to use minAge with a static constant which is set from database when application is starting. In this situation I strongly believe that it should be definitely possible passing either values or expressions to validate a field.
Custom constraint is a feasible solution however I was expecting better intuitive solutions.
From these comments I can understand that this is not possible cross field validation or passing another field value without using reflection with custom annotations or type level validation.
Thanks
s.grinovero wrote:
Quote:
If I have two fields like minAge and maxAge, how can I validate that maximum age is greater or equal to minumum age?
Think about who is the subject you're validating. Is it minAge? or is it maxAge? Should you validate minAge value according to the maxAge value or the other way around?
Cross-field validation doesn't make sense, as you're not validating the fields but you're validating correctness of the entity containing them; It's the entity which is not valid when it has a maxAge property lower than a minAge property; the fields themselves are not invalid per se.
The conceptually correct solution is to use a custom constraint on the containing type:
http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/#d0e328