Spring mvc annotation validation custom annotation
I have an @Password annotation that validates input for a valid password. First I would like to check with @NotEmpty before even calling @Password to get better error开发者_运维问答 messages.
If i use @NotEmpty @Password String password;
I will get two different validation errors.
I have tried with groups but then all fields belonging to the group must pass validation before validating the Password.
Is there a good way to solve this problem? Currently I have included the @Not Empty in the @Password implementation but is this the best way to do it?
I guess @Password
is a custom contraint that you have. If you rather want to display a common or aggregated error message, irrespective of which validation failed, then you may want to use @ReportAsSingleViolation
...
@NotNull
@Size(min = 6)
@ReportAsSingleViolation
public @interface Password {
...
Irrespective of which validation failed, this will display the message associated with @Password
. Here a generic message can be specified.
精彩评论