-->
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.  [ 4 posts ] 
Author Message
 Post subject: Validation one field against another
PostPosted: Tue Sep 25, 2007 3:26 pm 
Newbie

Joined: Wed Sep 19, 2007 10:05 am
Posts: 3
Hi,

I have quite a simple issue:
One entity (let's name it History) has 2 fields:
Date validFrom;
Date validTo;

I want to ensure that validTo is after validFrom.

My first idea was to define my own field validator that I put on validTo but I can only access the value from the property itself, thus not possible to inspect validFrom...

Of course I can write a method like that:
@AssertTrue
public boolean validateToAfterFrom() {
return validTo.after(validFrom);
}


but then, the validation is entity wide and I cannot know on my screen that I have to write an error message near validTo specifically...


Any solution to this problem that seems really standard ??
(either be able to inspect the whole entity or be able to specify on which field an AssertTrue function works)


Thx very much

vincent

Hibernate version: 3.2.2 GA
Hibernate Validator version: 3.0.0 GA


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 11:34 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
you can use a bean level validator

_________________
Emmanuel


Top
 Profile  
 
 Post subject: well
PostPosted: Tue Oct 02, 2007 7:56 am 
Newbie

Joined: Wed Sep 19, 2007 10:05 am
Posts: 3
Thx for the answer, but...

With a bean level validator, I get for sure the whole bean in parameter, so I am able to do the validation, but it is not possible to state that a given field is in error... and this is the behavior you expect (excerpt from hibernate validator unit test:
Code:
public void testBeanValidator() throws Exception {
      Vase v = new Vase();
      ClassValidator<Vase> classValidator = new ClassValidator<Vase>( Vase.class );
      InvalidValue[] invalidValues = classValidator.getInvalidValues( v );
      assertEquals( 1, invalidValues.length );
      assertNull( invalidValues[0].getPropertyName() );//<<<<<<
   }


Note: this is the same issue as when using @AssertTrue on a method


I really need to validate ONE field, but for this validation, I need to access other fields...

One implementation suggestion (actually I already did it and it works fine...):
* add one interface
Code:
public interface ValidatorWholeBeanAware{}

* flag my validator with it:
Code:
public class MyValidator implements Validator<Assert>,ValidatorWholeBeanAware

* in ClassValidator around line 385 and 505 (methods= getInvalidValues(T, circ) and getInvalidValues(T, propertyName)):

replace
Code:
Object value = getMemberValue( bean, getter );


by
Code:
Object value=bean;
if (!(validator instanceof ValidatorWholeBeanAware)) {
   value = getMemberValue( bean, getter );
}


Then MyValidator is still a property level validator (ie, InvalidValue.getPropertyName() will return the field name) but it can access the whole bean...

What do you think of this implementation ?

Thx very much

vincent


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 19, 2007 5:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The proposal is elegant but...
If you push your usecase, it's not really validating one field, it's really validating a tuple. You might want to give back a message like:

date from: 2005 should be older than date to: 2004

thus getPropertyName() not making much sense. Why do you need it (the property name)?

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.