6
我怎麼能去在下列情況下重寫驗證上的電子郵件爲AuthorizedUser:JSR 303的驗證覆蓋
public class Account {
@Length(min = 1, max = 100,
message = "'Email' must be between 1 and 100 characters in length.")
@NotNull(message = "'Email' must not be empty.")
protected String email;
@Length(min = 1, max = 50,
message = "'Name' must be between 1 and 50 characters in length.")
private String name;
}
public class AuthorizedUser extends Account {
@Length(min = 1, max = 40,
message = "'Field' must be between 1 and 50 characters in length.")
private String field;
}
我知道由二傳手覆蓋的電子郵件地址,我可以「砍」的解決方案對通過執行AuthorizedUser如下:
@Override
public void setEmail(String email) {
this.email = email;
super.setEmail(" ");
}
我就覺得骯髒...這是可能的,而無需編寫自定義驗證程序被重寫?
我試着將@Valid移動到超類的setter中,並將它放在覆蓋的字段中,但仍然收到超類中關於它爲空的消息。有沒有更懶惰的方法來做到這一點?
到目前爲止,我結束了編寫自定義驗證,在我簡單的例子下面的情況下工作,但在更復雜的模型中,我認爲需要製作更多的課程是沒有意義的。 – Scott