How to set check order for JSR303 bean validation

后端 未结 3 1878
情书的邮戳
情书的邮戳 2021-01-01 03:57

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          


        
3条回答
  •  佛祖请我去吃肉
    2021-01-01 04:14

    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.

提交回复
热议问题