-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Validating two fields together
PostPosted: Wed Feb 03, 2010 2:52 pm 
Newbie

Joined: Tue Feb 02, 2010 9:00 am
Posts: 19
Hi,

I have little difficulties to use validation api. How can I validate two dependent field together? I should use class level validation or something else?

As an example:

1) If I have two fields like minAge and maxAge, how can I validate that maximum age is greater or equal to minumum age?

Thanks


Top
 Profile  
 
 Post subject: Re: Validating two fields together
PostPosted: Wed Feb 03, 2010 3:46 pm 
Newbie

Joined: Tue Feb 02, 2010 9:00 am
Posts: 19
Sessizlik wrote:
Hi,

I have little difficulties to use validation api. How can I validate two dependent field together? I should use class level validation or something else?

As an example:

1) If I have two fields like minAge and maxAge, how can I validate that maximum age is greater or equal to minumum age?

Thanks


From my understanding, cross-field validation is not possible. But what is the reason for this?

Thanks


Top
 Profile  
 
 Post subject: Re: Validating two fields together
PostPosted: Wed Feb 17, 2010 6:52 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
Cross-Field validation is possible using Apache MyFaces Extention Validator (ExtVal) for JSF (1.1 - 1.2 only i supose).

The next version of ExtVal, now in the works, supports the Bean Validation spec, and will thus probably be compliant with Hibernate Validator 4.x.
Maybe you can take a look at the ExtVal snapshots / sandbox versions and try to use those.

Otherwise, I don't know...perhaps only a custom constraint might be possible ... but then how would you access the other property of the class?


Top
 Profile  
 
 Post subject: Re: Validating two fields together
PostPosted: Sun Feb 21, 2010 8:18 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
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

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Validating two fields together
PostPosted: Sun Feb 21, 2010 9:06 am 
Newbie

Joined: Tue Feb 02, 2010 9:00 am
Posts: 19
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


Top
 Profile  
 
 Post subject: Re: Validating two fields together
PostPosted: Sun Feb 21, 2010 9:17 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
There's no reflection involved at all, implementing a custom constraint is trivial.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.