Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3 MVC - form:errors not showing the errors

Tags:

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.

like image 822
Eqbal Avatar asked May 18 '10 20:05

Eqbal


3 Answers

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

like image 138
Eqbal Avatar answered Sep 17 '22 15:09

Eqbal


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"; } 
like image 31
mokshino Avatar answered Sep 20 '22 15:09

mokshino


I faced the same issue.

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

like image 39
Ahamed Mustafa Avatar answered Sep 19 '22 15:09

Ahamed Mustafa