...more:
The group approach work with my first example. Rewriting:
Code:
@NotNull
@Length(groups="user", min=4, max=20)
@Length(groups="admin", min=8, max=20)
public String getPassword() {
return password;
}
The java annotation has another problem to put more than one @Length in the same field, but that is another problem. Probably the right would be:
@LengthConstraints(
@Length(groups="user", min=4, max=20)
@Length(groups="admin", min=8, max=20)
)
I had ignored this limitation in examples...
But ignoring that the important thing is the group approach doesn't work with the second example. Rewriting:
Code:
public Boolean isReceiveNews() {
return receiveNews;
}
public Boolean isReceiveAlerts() {
return receiveAlerts;
}
@NotNull(groups={"news","alerts"})
public String getMail() {
return mail;
}
@NotNull(groups={"alerts"})
public String getTelephone() {
return telephone;
}
The groups are pre-defined in the code (fixed)... and validation conditional I suggested in the first post depends on the data of the bean.
Without evaluating the data of mybean.isReceiveNews() and mybean.isReceiveAlerts() I don't know if I have to call:
validator.validate(mybean, "news") or validator.validate(mybean, "alerts")
Then the groups approach is a validation conditional immutable / static.
The original proposal of the topic is a validation conditional "dynamic" based on the data. The groups approach not solve this because the groups and 'validate if' are pre-defined.