Spring MVC Validation with annotation not working in nested fields

Let's go straight to my classes Person.java

public class Person {

    @Size(min=4, max=10)
    private String name;

    @Range(min=0, max=80)
    private Integer age;

    @NotEmpty
    private String address;

    @Pattern(regexp="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")
    private String email;

    @Past
    private Date birth;

    private Pet pet;
...
}

and Pet.java

public class Pet {

    @NotEmpty
    public String name;
    @NotEmpty
    public Integer age;
...
}

and this is my form

   <form:form action="enter" method="post" commandName="personForm">
name:   <form:input  path="name"/>      <form:errors path="name" />     <br/>
age:    <form:input  path="age"/>       <form:errors path="age" />      <br/>
address:<form:input  path="address"/>   <form:errors path="address" />  <br/>
email:  <form:input  path="email"/>     <form:errors path="email" />    <br/>
birth:  <form:input  path="birth"/>     <form:errors path="birth" />    <br/>
petName:<form:input  path="pet.name"/>  <form:errors path="pet.name" /> <br/>
petAge: <form:input  path="pet.age"/>   <form:errors path="pet.age" />  <br/>
<input type="submit" value="Register"/> <br/>
</form:form>

The code works well and shows message at form:error.. block when invalid values is entered except when it comes to petName and petAge.

Actually, the code doesn't validate petName and petAge at all. I'm using hibernate validator. Could Someone tell me How to solve this?

Answers


Try adding javax.validation.Valid annotation on top of pet.

public class Person {

    @Size(min=4, max=10)
    private String name;

    @Range(min=0, max=80)
    private Integer age;

    @NotEmpty
    private String address;

    @Pattern(regexp="^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")
    private String email;

    @Past
    private Date birth;

    @Valid  // This line   
    private Pet pet;

}

Need Your Help

Python to parser a web page's images URLs

python regex urllib

This is my code to get a web page's image's URLs

Event for SQLcommand finished

c# sql wpf data-binding datagrid

Is there an event that will tell me when a SQL query has finished. I am populating a table view using the following code.