Search code examples
hibernate-validator

Hibernate Validation Message Not Displayed


I have prepared an application that works with Form Validations using

Spring MVC and Hibernate.

I have added the @NotNull constraint on my 'salary' field, with a message

'Please enter proper value', but that message is not getting displayed

How do I resolve this??

messages.properties :

NotNull.person.salary=should not be null

model

 @Entity
public class Person {

    @Id     
    @GeneratedValue(strategy=GenerationType.AUTO)   
    private Integer id;
    private String name;    
    private String address;
    @Valid @NumberFormat(style=Style.CURRENCY) @NotNull(message="Please enter a value")
    private int salary;
    private String gender;

Form

form:form action="/MainAssignment3/save"  method="post" modelAttribute="p">
        <!-- <input type="hidden" name="id" > -->
        name : <input type="text" name="name"> <br/>
        address : <input type="text" name="address" ><br/>
        gender : <input type="text" name="gender" ><br/>
        salary : <input type="text" name="salary" ><br/>
        <input type="submit"value="submit">
    </form:form>

AddPerson.jsp

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Employee</title>
</head>
<body>
<springForm:form action="/MainAssignment3/save" method="POST"
        commandName="person">

        <table style="border: thin; border: ridge;">

            <tr>
                <td>name</td>
                <td><springForm:input path="name" id="name"
                        placeholder="Enter Name" /></td>

            </tr>
            <tr>
                <td>address</td>
                <td><input type="text" name="address" placeholder="Enter Address" ></td>
            </tr>
            <tr>
                <td>gender</td>
                <td><springForm:input path="gender" id="gender" placeholder="Enter Gender" /></td>

            </tr>
            <tr>
                <td>salary</td>
                <td><input type="text" name="salary" placeholder="Enter Salary" ></td>
                <td><springForm:errors path="salary" /></td>


                <td style="padding-left: 110px;"><input type="submit"value="submit"></td>
            </tr>

        </table>
    </springForm:form>

messages.properties

NotNull.p.salary=invalid

//////////////////////////Controller/////////////////

@RequestMapping(value = "/newPerson", method = RequestMethod.GET)
public ModelAndView newPerson(ModelAndView model) throws IOException {
    @SuppressWarnings("unused")
    Person p = new Person();
    model.addObject("person", new Person());
    model.setViewName("AddPerson");

    return new ModelAndView("AddPerson", "person", new Person());

}

@RequestMapping(value="/save")
public String save(@ModelAttribute @Valid Person p, BindingResult result) {

    if(result.hasErrors())
    {

        return "AddPerson";
    }
    else
    {       
    ps.save(p);

    return "redirect:http://localhost:8080/MainAssignment3/";

    }
}

Solution

  • Please change the data type of 'salary' from int to Integer. You will need a wrapper class for this validation to work. You can refer this http://forum.spring.io/forum/spring-projects/web/80192-validation-empty-int-field