Reputation: 2136
I have two fields in my bean
String key,
String value,
When field key="A" , "value" should follow a particular Regex for other "key" - it can be anything.
How would I define this validation on value based on key.
Upvotes: 0
Views: 246
Reputation: 2230
You can use class-level constraints.
1- Annotate your bean with class-level custom constraint annotation:
@ValidKeyValue
public class MyBean {
private String key;
private String value;
...
}
2- Create the custom annotation and its validator.
3- Implement your validation logic in the isValid
method:
@Override
public boolean isValid(MyBean myBean, ConstraintValidatorContext context) {
if ("A".equals(myBean.getKey())) {
// case 1
} else {
// case 2
}
}
Upvotes: 2