21

I am using annotation based validation but for one of the forms I am not able to show any errors using the form:errors tag. When I debug the method, I can see the BindingResult has errors, but for some reason its not being displayed on the form. I am stumped as I have got it working on other forms, but for some reason this particular form is having issues. Any pointers are greatly appreciated.

Here is some code from the controller, I have the copyCartForm as a @SessionAttribute as well in the Controller:

@RequestMapping(params="action=Confirm Copy", method=RequestMethod.POST)
public String copyCart(@Valid CopyCart copyCartForm, BindingResult result) {
    if (result.hasErrors()) {
        logger.debug("errors in form" + result.toString());
        return "copyshoppingcart";
    } else {
                    ...
                    ...
        return "redirect:/home";
    }
}

In the JSP I have tried this:

<form:errors path="*" cssClass="formError"/>

as well as:

<form:errors path="fieldName" cssClass="formError"/>

Neither works.

Eqbal
  • 4,722
  • 12
  • 38
  • 47
  • Better show us your JSP code. – Jacob Mattison May 18 '10 at 20:12
  • 3
    I had to use `@ModelAttribute` to get this working. So the form was preceded by `@ModelAttribute("copyCartForm") @Valid CopyCart copyCartForm, BindingResult result)` Not sure why though? At other places it works without that. – Eqbal May 18 '10 at 22:29

5 Answers5

43

I had to use @ModelAttribute to get this working. So the form was preceded by @ModelAttribute("copyCartForm") @Valid CopyCart copyCartForm, BindingResult result)

Eqbal
  • 4,722
  • 12
  • 38
  • 47
  • 8
    This worked for me. It seems that if your model attribute name (copyCartForm) doesn't match the class name of the model (copyCart) that you have to specify it as a parameter to the @ModelAttribute attribute. – Josh Aug 23 '11 at 11:56
  • 1
    Hey! Thanks, I'm looking into this right now and my bindingResult.hasErrors() doesn't seem to be returning anything even though I've added annotations on the model such as notnull etc. – Michael Dec 11 '16 at 15:56
9

One more approach, If for some reason you cannot use @ModelAttribute("copyCartForm") when use follow:

@RequestMapping(method=RequestMethod.POST)
public String post(@Valid CopyCart copyCartForm, BindingResult bindingResult, ModelMap modelMap) {
    if (bindingResult.hasErrors()) {
        modelMap.put(BindingResult.class.getName() + ".copyCartForm", bindingResult);
        return "copyshoppingcart";
    }
    return "redirect:/home";
}
mokshino
  • 1,435
  • 16
  • 11
6

I faced the same issue.

I had to use the @ModelAttribute("attributeName") to get the validation error back in response.

stema
  • 90,351
  • 20
  • 107
  • 135
2

I am not sure why and I suffered a lot until I figured it out, but you HAVE TO name your bean exactly as your bean class. So copyCartForm should be just copyCart.

@RequestMapping(params="action=Confirm Copy", method=RequestMethod.POST)
public String copyCart(@Valid CopyCart copyCart, BindingResult result) {
    if (result.hasErrors()) {
        logger.debug("errors in form" + result.toString());
        return "copyshoppingcart";
    } else {
                    ...
                    ...
        return "redirect:/home";
    }
}

This must also be changed on the form tag:

<form:form action="....." method="..." commandName="copyCart">
0

you need to add this dependency to your pom.xml:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
</dependencies>
ismailalabou
  • 37
  • 2
  • 8