0

在我正在处理的当前项目中,模型 bean 在 3 个字段中有电话号码,但我们只想要一个弹簧验证错误。可以这样做吗>

@NotEmpty(message = "Your Question must not be blank.")
@Size(min = 10, max = 200)
private String content;

@NotEmpty(message = "Area Code must not be blank.")
@Size(min = 3, max = 3, message = "Area must be 3 numbers")
private String areacode;

@NotEmpty(message = "Phone Number must not be blank.")
@Size(min = 3, max = 3, message = "phone number first part must be 3 numbers")
private String phone3;

@NotEmpty(message = "Phone Number must not be blank.")
@Size(min = 4, max = 4, message = "Phone number last part must be 4 numbers")
private String phone4;
4

1 回答 1

1

如果无论何时验证错误发生在类级别而不是在字段中都无关紧要,那么您可以使用休眠验证ScriptAssert Validation

@ScriptAssert(
     lang="javascript"
     script="_this.areacode!=null and _this.phone3!=null and _this.phone4!=null"
 )
public class YourClass {
     private String areacode;
     private String phone3;
     private String phone4;
}

有关更多其他想法,请查看跨域验证讨论/问题。 使用 Hibernate Validator (JSR 303) 进行跨字段验证

于 2013-03-07T16:42:19.680 回答