I was using JSR 303 validation with hibernate validator to have fields validated automatically by specifying @Valid on the controller method. Validation was working fine. I have know added an upload field to the form and have added a @RequestParam(“file”) as a MultipartFile file. Now it works only if all fields are valid on submission otherwise I get a 404 (Bad Request). If I remove the @Valid annotation I get the javax.validation.ConstraintViolationException with all the validation violations with Status 500.
I’m using Spring 3.2
my form:
<form action="#springUrl("/admin/stores/save")" method="POST" enctype="multipart/form-data">
Name:
#springBind( "store.name" )
<input type="text"
name="${status.expression}"
value="$!status.value" /><br>
......
<input type="file" name="file" accept="image/*">
<input type="submit" value="submit"/>
</form>
Controller:
@RequestMapping(value="/save", method = RequestMethod.POST)
@Transactional
public String save(@Valid @ModelAttribute Store store, @RequestParam("file") MultipartFile file, BindingResult bindingResult, ModelMap model) {
if (bindingResult.hasErrors()) {
model.addAttribute("message", "Failed");
model.addAttribute("store", store);
return "admin/stores/form";
} else {
.....
your problem is in method argument order.
@ModelAttributemust be followed byBindingResultargument. Look at Spring documentation and check also Example 17.1. Invalid ordering of BindingResult and @ModelAttribute.. You also should add
MultipartFileto form class (Store) if it is part of it.