java - BindingResult isn't detecting annotation based constraints and errors -
the validation errors don't show reason. in fact, binding result doesn't return errors. bean follows:
@notblank @size(min = 5, max = 20, message = "user id must 5 20 characters long.") @pattern(regexp = "^[a-za-z0-9]+$", message = "user id must alphanumeric.") public string getusername() { return username; } public void setusername(string username) { this.username = username; } @notblank @size(min = 6, max = 20, message = "the password must between 6 20 characters long.") public string getpassword() { return password; } public void setpassword(string password) { this.password = password; }
my controller class has following variable:
private beanvalidator beanvalidator = new beanvalidator(new annotationbeanvalidationconfigurationloader());
there method in controller handles binding.
beanvalidator.validate(userlogin, bindingresult); if(bindingresult.haserrors()) { return "/"; } else return "/homepage";
when login using blank credentials values of username , password, system logs in. evidently, bindingresult isn't detecting errors. help?
i used @valid
instead of implementing own validator. also, parameter has in following order:
public string homepage(@modelattribute(value="userlogin") @valid userlogin userlogin, bindingresult bindingresult, model model) {//your code}
mind order of @valid
bean parameter , bindingresult
parameter. bindingresult
should placed after @valid
parameter otherwise error displayed.
Comments
Post a Comment