-->
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.  [ 8 posts ] 
Author Message
 Post subject: Validation rule processing order
PostPosted: Mon Feb 18, 2008 8:12 am 
Newbie

Joined: Fri Sep 26, 2003 1:50 pm
Posts: 9
Hello,

Does the validator apply the rules in any specific order?

For example, if I have a @NotNull rule on a variable, and use that variable in a method with @AssertTrue (or a custom validator), can I trust the variable to be non-null? If the order is indeterminant, then so is the message reported (assuming each rule has a different message).

/Thanks, Ross.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 8:19 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The order is not guaranteed to be deterministic. I am not sure why it's important in your case though. The list of failures will be deterministic.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 9:28 pm 
Newbie

Joined: Fri Sep 26, 2003 1:50 pm
Posts: 9
Thanks Emmanuel,

You are correct that the set of reported messages will be deterministic. My mistake!

My main concern was when writing assertions that test fields with their own rules (such as @NotNull) that I avoid cascading messages (i.e. I only want to report an error for the root cause). What I am doing in the assertions now is returning true if certain preconditions are not met, and only return false for the error case for which the assertion is intended.

Are there any guidelines as to when it is considered good practice to use the validator system compared to writing code directly in the setter method (in the domain model)?

/Cheers, Ross.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 1:09 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
your approach is correct (ie ignore the preconditions).

Annotations are much more readable, can be checked all at once and get error report, can be reused by other frameworks to share the same constraints, is usually much more consistent esp wrt exceptions raised etc.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Validation rule processing order
PostPosted: Wed Jan 20, 2010 1:42 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
Hi all.

I use @AssertTrue annotation to ensure the execution of a method that sets some default values (always returns true). These setted values are validated as @NotEmpty (these are Strings). So I need to guarantee that method annotated with @AssertTrue is executed strictly before that fields annotated with @NotEmpty.

Simplified code example (not included Hibernate annotations):

Code:
public class MyClass {
   @NotEmpty
   private String myField = null;

   @SuppressWarnings("unused")
   @AssertTrue
   private boolean fillDefaultValues() {

      if (this.myField == null) {
         this.myField = "default value";
      }

      return true;
   }
}

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: Validation rule processing order
PostPosted: Wed Jan 20, 2010 1:57 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The best is to use a group sequence for that.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Validation rule processing order
PostPosted: Wed Jan 20, 2010 3:38 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
I'm afraid groups only works with Hibernate Validator version 4 (e.g. 4.0.2.GA), but I use 3.1.0.GA.

If I update my dependencies I could use groups.

In version 3.1.0, is there any way to indicate my constraint as bean validation, and not member validation?

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: Validation rule processing order
PostPosted: Thu Jan 21, 2010 4:10 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Alberthoven wrote:
I'm afraid groups only works with Hibernate Validator version 4 (e.g. 4.0.2.GA), but I use 3.1.0.GA.

If I update my dependencies I could use groups.

In version 3.1.0, is there any way to indicate my constraint as bean validation, and not member validation?


Out of the box you can add Hibernate Validator 4 alongside Hibernate Validator 3.1 but they will ignore each other. If you are looking for the smooth integration with Seam etc, then upgrading to 4 is not what you want unless you go for JBoss AS 6 M1 or above.
Yes version 3.1 does support class-level validation.

_________________
Emmanuel


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