如何在类级别约束之前验证字段级别约束? [英] How to validate field level constraint before class level constraint?
问题描述
我有一个课程:
@ColumnNameUnique(groups = CreateTableChecks.class)
public class Table {
@Valid
@NotEmpty(groups = CreateTableChecks.class)
private List<Measure> measures;
}
-
班级约束
@ColumnNameUnique(groups = CreateTableChecks.class)
总是首先运行,之后是字段级约束@NoEmpty(groups = CreateTableChecks.class)
运行。The class level constraint
@ColumnNameUnique(groups = CreateTableChecks.class)
always runs first, after that the field level constraint@NotEmpty(groups = CreateTableChecks.class)
runs.无论如何强制字段级约束
@NoEmpty(groups = CreateTableChecks.class)
先运行?Is there anyway to force the the field level constraint
@NotEmpty(groups = CreateTableChecks.class)
runs first?推荐答案
你需要使用
@GroupSequence
和重新定义默认组序列的。如果没有这个,组中的验证顺序就不会被定义,它可以是任何顺序(在你的情况下,总是先执行类级别约束不是必须的)。这样的事情应该有效: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<Measure> measures; }
现在,如果
@Default
group得到验证,首先是类级别约束,然后是字段级别约束。Now, if the
@Default
group gets validated, first the class level constraints and then the field level ones will be validated.这篇关于如何在类级别约束之前验证字段级别约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!