views:

58

answers:

2

I use the JSR303 Bean Validation to check the form input.

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

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

when then name = '' and email = '',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, for example.

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

(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 a custom annotation for every property)

And I also consider the validation group and group sequence, but i think there are some difference for my need.

@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;

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.

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

// 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;
    }
  }
}

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

+1  A: 

Yes Bean Validation supports this feature. It is called validation groups and group sequences. Groups are simple marker interfaces, for example you could create the two interfaces First and Second and change your code to something like this:

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

Then you can define a group sequence:

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

Get hold of the Validator and call validator.validate(myObject, All.class)

It's all in the documentation.

Hardy
Very thank you for your reply, group sequence is do not meet my need, i edit my question.
eckelcn
A: 

Have you considered to define two sequences. One for name and one for mail?

@GroupSequence({NameFirst.class, NameSecond.class})
interface AllName {
}

@GroupSequence({MailFirst.class, MailSecond.class, MailThird.class})
interface AllMail {
}

In this case you would call the validator like this:

validator.validate(myObject, AllName.class, AllMail.class)
Hardy