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?