Reputation: 4719
I trying to validate a simple model by following some example code. I got following exception.
Here is Model:-
@Entity
public class Customer{
String password;
String confirmPassword;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirmPassword() {
return confirmPassword;
}
public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}
}
Validator class:-
@Component
public class PasswordValidator implements Validator{
@Override
public boolean supports(Class clazz) {
//just validate the Customer instances
return Customer.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
"required.password", "Field name is required.");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword",
"required.confirmPassword", "Field name is required.");
Customer cust = (Customer)target;
if(!(cust.getPassword().equals(cust.getConfirmPassword()))){
errors.rejectValue("password", "notmatch.password");
}
}
}
Controller class that usages :-
@Controller
public class PasswordController {
@Autowired
private UserValidator userValidator;
@InitBinder
private void initBinder(WebDataBinder binder) {
binder.setValidator(userValidator);
}
@RequestMapping("/password")
public ModelAndView getPssword() {
Customer customer = new Customer();//(Customer)command;
return new ModelAndView("customerForm","customerForm",customer);
}
@RequestMapping(value="/password", method=RequestMethod.POST)
public ModelAndView restPostEditUser(@ModelAttribute @Validated Customer customer,
BindingResult result) {
if (result.hasErrors()){
return new ModelAndView("customerForm");
}
ModelAndView model = new ModelAndView("CustomerSuccess");
model.addObject("customer", customer);;
return model;
}
}
I got following exception:-
HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalStateException: Invalid target for Validator [com.fnx.reg.validator.UserValidator@1aeb77d1]: com.fnx.reg.model.Customer@2ca7c795
exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Invalid target for Validator [com.fnx.reg.validator.UserValidator@1aeb77d1]: com.fnx.reg.model.Customer@2ca7c795
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.IllegalStateException: Invalid target for Validator [com.fnx.reg.validator.UserValidator@1aeb77d1]: com.fnx.reg.model.Customer@2ca7c795
org.springframework.validation.DataBinder.assertValidators(DataBinder.java:495)
org.springframework.validation.DataBinder.setValidator(DataBinder.java:486)
com.fnx.reg.controller.PasswordController.initBinder(PasswordController.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Upvotes: 0
Views: 1292
Reputation: 28519
Seems that you're setting a UserValidator
instead of PasswordValidator
to your binder
Upvotes: 2