-->
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: Control validation annotations order ?
PostPosted: Sun Apr 10, 2011 2:45 pm 
Newbie

Joined: Sun Apr 10, 2011 2:41 pm
Posts: 1
hi guys, i have a field that have two validation annotations
Code:
    @NotEmpty
    @Length(min=3,max=100)
    String firstName;


i am just curious to know how hibernate is specifying the order of which validation annotation to be executed first, and if it's possible to custom that ?

the reason i am asking is that if i left that field empty sometimes the first validation message displayed is for not empty and other times if i left it empty i get the validation message for the length annotation.

thanks in advance.


Top
 Profile  
 
 Post subject: Re: Control validation annotations order ?
PostPosted: Mon Apr 11, 2011 4:01 am 
Newbie

Joined: Mon Apr 11, 2011 3:47 am
Posts: 1
Location: San Jose, CA
I am not much familiar with this. but i was trying to search for your query and look what i have found :)

If no groups are specified a constraint is part of the Default Bean Validation group (see: javax.validation.groups.Default).

Create an interface to be your "Extended" (or whatever you want to call it) group:

public interface Extended{}

Now create an interface that will have the javax.validation.GroupSequence annotation.

@GroupSequence({Default.class, Extended.class})
public interface MySequence {}

Set the validation groups on your constraints

@NotEmpty // If no group is specified it is part of the default group
@Length(min=3,max=100, groups = Extended.class)
String firstName;

Pass MySequence to your validator call.

validator.validate(object, MySequence.class);

As specified by your @GroupSequence the default constraints will be validated first and if no contraint violations are encountered it will move on to the extended group.

_________________
SSL Certificates : Now buy an SSL Certificate at $7.99/yr.


Top
 Profile  
 
 Post subject: Re: Control validation annotations order ?
PostPosted: Mon Apr 11, 2011 4:23 am 
Contributor
Contributor

Joined: Mon Feb 28, 2011 7:27 pm
Posts: 20
Location: France
Hi,

As the Bean Validation specification says (Hibernate Validator is the Reference Implementation) the constraints are evaluated in no particular order (see section 3.4.2 of the specification). However, it's possible to specify an order of evaluation. To achieve that, you have to work with GroupSequence and groups as says hillaryjohnson (a GroupSequence defines a list of groups which have to be evaluated in the specified order and a bit more :).

There is an example in the HV doc about this (http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/html_single/#d0e974).

Hope this help.

--Kevin


Top
 Profile  
 
 Post subject: Re: Control validation annotations order ?
PostPosted: Mon Apr 11, 2011 4:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

as mentioned before, if you want to enforce an order you have to use a group sequence.
And just as a reminder, the Java reflection API does not give you any information about the order in which you specify annotations. You can just find out which annotations are places on a method/field not in which order.

--Hardy


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.