开发者

Spring mvc annotation validation custom annotation

开发者 https://www.devze.com 2023-02-06 03:22 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消