Sunday 15 April 2012

java - JSR 303 Bean validation -



java - JSR 303 Bean validation -

i have 2 fields in bean

string key, string value,

when field key="a" , "value" should follow particular regex other "key" - can anything.

how define validation on value based on key.

you can utilize class-level constraints.

1- annotate bean class-level custom constraint annotation:

@validkeyvalue public class mybean { private string key; private string value; ... }

2- create custom annotation , validator.

3- implement validation logic in isvalid method:

@override public boolean isvalid(mybean mybean, constraintvalidatorcontext context) { if ("a".equals(mybean.getkey())) { // case 1 } else { // case 2 } }

java javabeans bean-validation

No comments:

Post a Comment