I'm having some trouble setting up validation for a form in spring.
The bean I would like to validate look like this:
public class RegistrationForm extends ProjectXUser {
@NotEmpty
private String password2;
@NotBlank
@AssertTrue
private Boolean agreedToConditions;
...
ProjectXUser inherits from BaseUser which has some more properties which are also annotated.
My controller looks like this:
@Controller
public class RegistrationController {
private static final String REGISTRATION_JSP = "registration";
@ModelAttribute("registrationForm")
public RegistrationForm getRegistrationForm() {
return new RegistrationForm();
}
@RequestMapping(value = { "/registratie/jaar", "registratie/proef" }, method = RequestMethod.GET)
public String year() {
return "registration";
}
@RequestMapping(value = { "/registratie/jaar", "registratie/proef" }, method = RequestMethod.POST)
public ModelAndView register(@Valid RegistrationForm registrationForm, BindingResult result) {
if (result.hasErrors()) {
return new ModelAndView(REGISTRATION_JSP);
} else {
return new ModelAndView("redirect:/registratie/success");
}
}
}
My spring configuration file contains:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<mvc:annotation-driven />
I've read in the spring documentation that if a jsr-303 validator is present in the class path spring will detect it automatically and use it. So i've added hibernate-validator to my pom.
But when I debug my controller I can see the registrationForm contains the values I've filled in. But results always has 0 errors. Even if I enter some explicit wrong input in my form fields.
validator.validate(registrationForm), does it work ? - tbruyelle