0

I have a situation similar to the followa:

public class Shop {
  @NotNull
  String name;
  @NotNull
  String desc;
}

a button to show a form where I have to insert my user:

<form:form method="post" action="saveShop.html" modelAttribute="Shop">

<form:label path="name">Name:</form:label>
<form:input path="name" value="${shop.name}" />
<form:label path="desc">desc:</form:label>
<form:input path="desc" value="${shop.desc}" />
<input type="submit" value="Submit"/>
</form:form>

Controller:

@RequestMapping("/addShop")
public ModelAndView LoadFormPage(@ModelAttribute("Shop")Shop shop) {
    ModelAndView model = new ModelAndView();
    model.setViewName("/shop/addShop");
    return model;
}

@RequestMapping(value = "/saveShop", method = RequestMethod.POST)  
public ModelAndView saveShop(@Valid @ModelAttribute("Shop") Shop shop, BindingResult result) {
    if (result.hasErrors()) { *do some*
    } else { *do someelse* }
}

Even if I leave all fields blank (I have tried even with different data type) and submit the form the controller never recognize errors.

Can you help me?

1 Answers1

0

Make sure to add below configs.

<bean id="myBeansValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

and

<mvc:annotation-driven validator="myBeansValidator">

and

<!-- Hibernate Validator -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.2.0.Final</version>
</dependency>

Adapted from Spring MVC form validation not working

Alien
  • 15,141
  • 6
  • 37
  • 57