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开发者_如何学Go)
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?
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.
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)
The order of validation related annotations / constraints cannot be specified at time of writing this answer. I've submitted order related issue to Hibernate Validator team a time ago, the issue was was accepted and probably will be solved in a next Bean Validation specification release - see more details here: http://beanvalidation.org/proposals/BVAL-248/ Btw, in simple cases you can avoid "order conflict" by using "non conflicting" combinations of annotations, e.g. NotNull
and Size
- Size will evaluate to true for null
value.
精彩评论