2

I'm trying to validate a form using Spring MVC, and it's working, but when the view containing the error messages comes, the fields I create through a forEach using an object I send from the controller aren't being created.

form.jsp

<form:form action="/project/product" method="POST" commandName="product">
    <div>
        <label>Title</label>
        <input type="text" name="title">
        <form:errors path="title"></form:errors>
    </div>
    <div>
        <label>Description</label>
        <textarea rows="10" cols="20" name="Description"></textarea>
        <form:errors path="Description"></form:errors>
    </div>
    <c:forEach items="${types}" var="priceType" varStatus="status">
        <div>
            <label>${priceType.nome}</label>
            <input type="text" name="prices[${status.index}].value"> 
            <input type="hidden" name="prices[${status.index}].type" value=${priceType }>   
        </div>
    </c:forEach>

    <button type="submit">Save</button>
</form:form>

Controller:

@RequestMapping("/form")
public ModelAndView form(ModelMap m) {
    m.addAttribute("product", new Product());
    ModelAndView modelAndView = new ModelAndView("products/form");
    modelAndView.addObject("types",PriceType.values());

    return modelAndView;
}

@RequestMapping(method=RequestMethod.POST)
public String grava(@Valid Product product, BindingResult result, RedirectAttributes redirectAtributes, ModelMap m) {
    if(result.hasErrors()) {
        return "products/form";
    }

    productDao.save(product);
    return "redirect:products/list";
}

Before submitting

After submitting

I've tried to return ModelAndView instead of String, no success, tried using "redirect:", but then even the validation messages don't work.

Rodrigo
  • 23
  • 4

2 Answers2

0

So that your @Valid annotation could work, you should have put javax.validation library and it is implementation (hibernate for example) into classpath. Also put some validation constraints on Product as well.

For example in maven i would put that two libs as dependencies:

<dependency>
  <groupId>javax.validation</groupId>
  <artifactId>validation-api</artifactId>
  <version>1.1.0.Final</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>5.1.0.Final</version>
</dependency>

And in Product class:

public class Product {
  @Min(2)
  @Max(255)
  private String title;
}
ernestk
  • 72
  • 6
  • Thank you, but the validation is actually working, I have those dependencies in my pom and the validation tags in my class.. The problem is that the fields that are created through the list that I add on ModelAndView as an object, aren't being created after the validation is done and the page is "refreshed"... – Rodrigo Feb 20 '16 at 17:42
  • maybe you can share code or add more description here. – ernestk Feb 20 '16 at 17:50
0

Instead of returning like this

if(result.hasErrors()) {
    return "products/form";
}

add the list object back

if(result.hasErrors()) {
ModelAndView modelAndView = new ModelAndView("products/form");
modelAndView.addObject("types",PriceType.values());
return "products/form";
 }

your will get the list back

Robert Ellis
  • 714
  • 6
  • 19