Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@initbinder not working for specific model attribute

I am using @valid and @initbinder for validation of the data being passed to the service but I am facing an issue that @InitBinder is working only globally,i.e.

@InitBinder // possible to leave off for global behavior
protected void initBinder(WebDataBinder binder){
    binder.setValidator(new LoginRequestValidator());
}

and not for a particular model attribute like I have an model object named LoginRequest :

@InitBinder("LoginRequest") // possible to leave off for global behavior
protected void initBinder(WebDataBinder binder){
    binder.setValidator(new LoginRequestValidator());
}

In this case the validator is not being called at all.. is this the right way to do this? or am I missing something?

like image 846
Mitaksh Gupta Avatar asked Jun 27 '13 11:06

Mitaksh Gupta


1 Answers

Looking at the signature of your method it has to be

@InitBinder("loginRequest")

with a small l

The value of @InitBinder can be one of the following

  • The name of a model attribute
  • The name of a request parameter
  • If none of the above apply, then the name of the class can be used, but starting with a small letter. That's how Spring exposes unnamed attributes to the model.
like image 156
a better oliver Avatar answered Oct 27 '22 01:10

a better oliver