-->
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: How to set check order for JSR303 bean validation
PostPosted: Thu Sep 30, 2010 8:31 am 
Newbie

Joined: Thu Sep 30, 2010 8:21 am
Posts: 5
I use the JSR303 Bean Validation to check the form input.

Code:
@NotBlank
@Size(min = 4, max = 30)
private String name;

@NotBlank
@Size(max = 100)
@Email
private String mail;

when then name and email is blankļ¼Œthe @NotBlank, @Size at name, @NotBlank, @Size, @Email at mail will be checked.

I want set the check order, when the previous order is invalid, the next is not checked,
like below.

Code:
@NotBlank(order = 1)
@Size(min = 4, max = 30, order = 2)
private String name;

(but the above is not support by JSR303)

Is there a way to implement it for using JSR303?
(I think the custom annotation will done, but i don't like to add so much custom annotation for every property.)


Last edited by eckelcn on Sun Oct 03, 2010 3:47 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Who to set check order for JSR303 bean validation
PostPosted: Thu Sep 30, 2010 3:36 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Did you already have a look at Bean Validation's concept of group sequences (see Hibernate Validator reference guide chapter 2.3.1)? This should do what you want.

Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Who to set check order for JSR303 bean validation
PostPosted: Thu Sep 30, 2010 8:56 pm 
Newbie

Joined: Thu Sep 30, 2010 8:21 am
Posts: 5
Firstly, very thank you for your reply.
I also consider the validation group and group sequence,
but i think there are some difference for my need.

Code:
    @NotBlank(groups = First.class)
    @Size(min = 4, max = 30, groups = Second.class)
    private String name;
   
    @NotBlank(groups = First.class)
    @Size(max = 100, groups = Second.class)
    @Email(groups = Third.class)
    private String mail;

    @GroupSequence({ First.class, Second.class, Third.class})
    interface All {
    }

When the name is blank and the mail is "abc".
Only the @NotBlank at name, and @NotBlank at mail will be checked by JSR303,
Because the @NotBlank at name is invalid, so the Second and Third group
is not be checked.

But my need is @NotBlank at name and @NotBlank, @Size, @Email at mail should be checked.
Because the @NotBlank at name is invalid, so the @Size at name is not be checked,
and the @NotBlank,@Size at mail is valid, so the @Email at mail is be checked.

Is there a good way to implement my need by JSR303?


Top
 Profile  
 
 Post subject: Re: Who to set check order for JSR303 bean validation
PostPosted: Fri Oct 01, 2010 3:37 am 
Newbie

Joined: Thu Sep 30, 2010 11:38 pm
Posts: 1
Thank you for the information guys...
Godspeed...how to deal with depression


Top
 Profile  
 
 Post subject: Re: Who to set check order for JSR303 bean validation
PostPosted: Fri Oct 01, 2010 10:40 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
eckelcn, I'm afraid I don't yet get your requirement right. I still think group sequences might help here, how about that:

Code:
    @NotBlank(groups = First.class)
    @Size(min = 4, max = 30, groups = Second.class)
    private String name;
   
    @NotBlank(groups = First.class)
    @Size(max = 100, groups = First.class)
    @Email(groups = First.class)
    private String mail;

    @GroupSequence({ First.class, Second.class })
    interface All {}


How is validation triggered in your case? Perhaps you also might invoke Validator#validate() manually for each group in the right order. Alternatively you might work with two sequences, which you validate independently from each other.

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Who to set check order for JSR303 bean validation
PostPosted: Fri Oct 01, 2010 10:57 am 
Newbie

Joined: Thu Sep 30, 2010 8:21 am
Posts: 5
According to the JSR303, Multi validation annotation can set on a field, for example:
Code:
    @NotBlank
    @Size(min = 4, max = 30)
    private String name;

Default, @NotBlank and @Size at name will all be checked by JSR303.
My requirement is when @NotBlank is invalid, @Size should not be checked.

Perhaps, the below pseudocode will be clear to describe my requirement.

Code:
// retrieves all fields and check one by one.
for (Field field : fields) {
  List annotations = getJsr303Annotations(field);
  // retrieves all annotations on field and check one by one.
  for (Annotation annotation : annotations) {
    CheckResult result = check(annotation, field);
    if (result.isValid()) {
       continue;
    } else {
       errors.add(result);
       break;
    }
  }
}


Top
 Profile  
 
 Post subject: Re: Who to set check order for JSR303 bean validation
PostPosted: Fri Oct 01, 2010 11:40 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
I see. But then my listing should fulfill your needs, right? When validating the "All" sequence, first the @NotBlank constraint will be checked, as it is part of the "First" group, and only if that succeeds, the @Size constraint will be evaluated, as it is part of the "Second" group.

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Who to set check order for JSR303 bean validation
PostPosted: Fri Oct 01, 2010 9:16 pm 
Newbie

Joined: Thu Sep 30, 2010 8:21 am
Posts: 5
Hi, Gunnar

Your code is:

Code:
    @NotBlank(groups = First.class)
    @Size(min = 4, max = 30, groups = Second.class)
    private String name;
   
    @NotBlank(groups = First.class)
    @Size(max = 100, groups = First.class)
    @Email(groups = First.class)
    private String mail;

    @GroupSequence({ First.class, Second.class })
    interface All {}


There are two case is not fit my requirement.
1. When the input mail is blank, and the name is "a", only the @NotBlank at mail's error will be catched, the @Size at name is not be checked (Because the First group is NG).
2. When the input mail's length is more then 100, the @Size, @Email at mail's error will all be catched.

So, I think the GroupSequence is not so good for my requirement.
Is there any other way to resolve the problem?


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.