2
votes

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.

2
I guess you using <context:component-scan base-package="" /> to make your @Controller works, but is your RegistrationForm class in the scanned package? - Arnaud Gourlay
Yes I'm using <context:component-scan base-package=""/>. It's scanneing org.x.y and my RegistrationForm is located in org.x.y.web.forms. So it should pick it up. - Jonas Geiregat
If you validate manually with validator.validate(registrationForm), does it work ? - tbruyelle
Wait, your form "contains the values I filled in", but it doesn't have errors? It looks like you're only validating that the fields have values. - dardo
Did you resolve the problem? I have the same problem except in my case, it returns "error 400" when the validation should fail and the code never goes to "if (result.hasErrors())" line. When the required fields are not blank, the code works fine. - Jayz

2 Answers

0
votes

You need to return the result in the ModelAndView in the case where there are errors. All you are returning is an empty mav.

See: http://forum.springsource.org/showthread.php?117436-Spring-MVC-3-and-returning-validation-errors-to-page-from-Valid&highlight=bindingresult for an example.

0
votes

If this bit of your code is firing and redirecting back to your registration page

if (result.hasErrors()) {
    return new ModelAndView(REGISTRATION_JSP);
}

Then your JSR-303 validation is being picked up. You need to display your errors on your JSP page like this

<form:errors path="password2" cssClass="error" />

where cssClass="error" is the CSS you want to display the error with. It's automatically put into a <div> for you.