java - Validator for MethodArgumentNotValidException only handles constraint of same type -


i'm trying validate form against constraints set on bean. spring-mvc version using 3.2.4. problem default spring validator not validate constraints; ones of same type.

i have following controller code:

@controller @sessionattributes() public class formsubmitcontroller {      @requestmapping(value = "/saveform", method = requestmethod.post)     @responsebody     public modelmap saveform(@valid @requestbody form form, httpsession session) {         session.setattribute("form", form);         modelmap map = new modelmap();         map.addattribute("haserrors", false);         return map;     } } 

and following bean:

public class form implements iform, serializable {      @notnull(message = "category should not empty")     protected string category;     @notnull(message = "sub-category should not empty")     protected string subcategory;      @size(min=0, message="firstname should not empty")     protected string firstname;     @size(min=0, message="lastname should not empty")     protected string lastname;     @pattern(regexp="^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d$",     message="date of birth should in dd-mm-jjjj format")     protected string dateofbirth;      //getters , setters } 

the handler methodargumentnotvalidexception looks this:

@controlleradvice public class formexceptioncontroller {      @exceptionhandler(methodargumentnotvalidexception.class)     @responsestatus(httpstatus.bad_request)     @responsebody     public modelmap     handlemethodargumentnotvalidexception(methodargumentnotvalidexception error) {         list<fielderror> errors = error.getbindingresult().getfielderrors();         modelmap map = new modelmap();         modelmap errormap = new modelmap();         map.addattribute("haserrors", true);         (fielderror fielderror : errors) {             errormap.addattribute(fielderror.getfield(), fielderror.getdefaultmessage());         }         map.addattribute("bindingerrors", errormap);         return map;     } } 

so, empty form results in first 2 error messages. firts 2 properties of form filled results in third , fourth error messages.

only when use same contraint type (i.e. notnull) properties on bean return error messages.

what can wrong here?

nothing wrong validator @size , @pattern default accept null valid. need both annotations (@notnull , @pattern/@size). these annotations trigger validation of values non-null, these validations don't imply null values invalid @notnull for.

this assuming using hibernate-vaildator (as out-of-the-box supported validator).


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -