Hibernate验证注释顺序优先级

问题描述 投票:0回答:1
public class AddressDTO {
   @NotNull
   @NotBlank 
   @Pattern(regexp="(0|91)?[789][0-9]{9}")
   String phoneNumber;
}

如何通过使用Hibernate注释为这些DTO类提供优先级验证?

java hibernate-validator
1个回答
0
投票

如果要按顺序验证约束并且仅对第一个失败的约束失败,则需要使用组和组序列。

请参阅有关如何使用组序列的文档的这一段:http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-defining-group-sequences

不过,它会有点冗长。

根据您的要求,另一种可能性是使用约束组合和@ReportAsSingleViolation。见http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-constraint-composition

最后一个(已经提到过)将使用您自己的验证代码编写自定义约束,以验证您的“约束”,并根据您检查的内容调整错误消息。

© www.soinside.com 2019 - 2024. All rights reserved.