I am getting following validation exception. I explored some of the answers earlier on same question but nothing worked for me.

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.rg.spring.model.User] during persist time for groups [javax.validation.groups.Default, ] List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='may not be empty', propertyPath=fname, rootBeanClass=class com.rg.spring.model.User, messageTemplate='{org.hibernate.validator.constraints.NotEmpty.message}'} ] org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973) org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)

In my model I have

@NotEmpty
@Column(name = "fname", nullable = false)
String fname;

Controller I have

@RequestMapping(value = { "/new" }, method = RequestMethod.POST)
public String saveUser(@Valid User user, BindingResult result, ModelMap model) {
        if (result.hasErrors()) {
            return "registration";
        }
        service.saveUser(user);
        model.addAttribute("success", "User " + user.getFname() + " registered successfully");
        return "success";
    }

I have following messages.properties

NotEmpty.user.fname = First Name is required!

Following dependency added in pom

<!-- jsr303 validation -->
<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.3.Final</version>
</dependency>

following is UI form

<form:form method="POST" modelAttribute="user">
    <form:input type="hidden" path="id" id="id"/>
    <table>
        <tr>
            <td><label for="fname">First Name: </label> </td>
            <td><form:input path="fname" id="fname"/></td>
            <td><form:errors path="fname" cssClass="error"/></td>
        </tr>

I have following method to load messages under AppConfig

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    return messageSource;
}
1

There are 1 best solutions below

0
On

Controller Class:

@RequestMapping(value = { "/new" }, method = RequestMethod.POST)
public String saveUser(@Valid @ModelAttribute("user") User user, BindingResult result,
        ModelMap model) {

    if (result.hasErrors()) {
        return "registration";
    }
    service.saveUser(user);
    model.addAttribute("success", "User " + user.getFname() + " registered successfully");
    return "success";
}

Add this method inside your controller class. This method removes whitespaces.

@InitBinder
public void InitBinder(WebDataBinder dataBinder) {
    StringTrimmerEditor stringTrimmerEditor = new StringTrimmerEditor(true);
    dataBinder.registerCustomEditor(String.class, stringTrimmerEditor);
}

I hope, this will work.