How to validate field level constraint before class level constraint?

后端 未结 2 1022
抹茶落季
抹茶落季 2021-01-06 23:36

I have a class:

@ColumnNameUnique(groups = CreateTableChecks.class)
public class Table {    
    @Valid
    @NotEmpty(groups = CreateTableChecks.class)
    p         


        
2条回答
  •  别那么骄傲
    2021-01-07 00:12

    You need to use @GroupSequence and re-define the default group sequence. Without this the validation order within a group is not defined and it can be in any order (that the class level constraint in your case is always executed first is not a must). Something like this should work:

    @GroupSequence({FieldChecks.class, ClassChecks.class})
    @ColumnNameUnique(groups = ClassChecks.class)
    public class Table {    
        @Valid
        @NotEmpty(groups = FieldChecks.class)
        private List measures; 
    }
    

    Now, if the @Default group gets validated, first the class level constraints and then the field level ones will be validated.

提交回复
热议问题