0

現在のプロジェクトでは、モデルBeanの3つのフィールドに電話番号がありますが、春の検証エラーが1つだけ必要です。これはできますか>

@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 に答える