Reputation: 2070
I am trying to validate some information, so I added a validator and used @Valid in the parameter of the post method:
@Controller
@RequestMapping("/user.htm")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(method = RequestMethod.GET)
public String userInfo(Model model) {
....
return "user";
}
@RequestMapping(method = RequestMethod.POST)
public String userInfoResult(@Valid @ModelAttribute UserForm userForm, BindingResult result, Model model ) {
UserInfo stat = userService.getStatitisque(userForm.getSearchCritera());
userForm.setListeExpediteur(listeExpediteur);
userForm.setUserInfo(stat);
model.addAttribute("userForm", userForm);
}
}
public class UserFormValidator implements Validator {
@Override
public boolean supports(Class<?> type) {
return UserForm.class.equals(type);
}
@Override
public void validate(Object o, Errors errors) {
UserForm userForm = (User) o;
...
}
}
When I debug, I never go in the UserFormValidator class.
Do I need to add something in these files?
web.xml
applicationContext.xml
dispatcher-servlet.xml
Upvotes: 4
Views: 7201
Reputation: 9
You need to add the validator in @InitBinder method
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new UserFormValidator ());
}
Upvotes: 0
Reputation: 298838
You need to add the validator in an @InitBinder
method:
@InitBinder(value="YourFormObjectName")
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new FooValidator());
}
or globally via XML:
<mvc:annotation-driven validator="globalValidator"/>
Reference:
Upvotes: 5
Reputation: 382
@Controller
@RequestMapping("/user.htm")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(method = RequestMethod.GET)
public String userInfo(Model model) {
....
return "user";
}
@RequestMapping(method = RequestMethod.POST)
public String userInfoResult(Model model,
@Valid @ModelAttribute UserForm userForm,
BindingResult result) {
/*
add custom validation check to standard validation error
(if not registry UserFormValidator in @InitBinder block)
*/
new UserFormValidator().validate(userForm, error);
// check all validation errors
if (errors.hasErrors()) {
// go back
return userInfo(model);
}
...
}
}
Upvotes: 0
Reputation: 24040
5.7.4.3 Configuring a JSR-303 Validator for use by Spring MVC
With JSR-303, a single javax.validation.Validator instance typically validates all model objects that declare validation constraints. To configure a JSR-303-backed Validator with Spring MVC, simply add a JSR-303 Provider, such as Hibernate Validator, to your classpath. Spring MVC will detect it and automatically enable JSR-303 support across all Controllers.
<mvc:annotation-driven/>
With this minimal configuration, anytime a @Valid @Controller input is encountered, it will be validated by the JSR-303 provider. JSR-303, in turn, will enforce any constraints declared against the input. Any ConstraintViolations will automatically be exposed as errors in the BindingResult renderable by standard Spring MVC form tags.
Upvotes: 0