Search code examples
javaspringspring-mvcbean-validationhibernate-validator

How to Validate different model class into one form using Spring MVC JSR-303 Validator


I have form inside jsp page as below:

<springForm:form action="${addAction}" name="frm" method="post" commandName="employee">
    <table>
        <tr>
            <td><label>First Name</label>   </td>
            <td><springForm:input path="firstName" /></td>
            <td><springForm:errors path="firstName" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>Last Name</label></td>
            <td><springForm:input path="lastName" /></td>
            <td><springForm:errors path="lastName" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>Email</label>    </td>
            <td><springForm:input path="email" /></td>
            <td><springForm:errors path="email" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>Street</label>   </td>
            <td><springForm:input path="employeeDetail.street" /></td>
            <td><springForm:errors path="employeeDetail.street" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>City</label> </td>
            <td><springForm:input path="employeeDetail.city" /></td>
            <td><springForm:errors path="employeeDetail.city" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>State</label>    </td>
            <td><springForm:input path="employeeDetail.state" /></td>
            <td><springForm:errors path="employeeDetail.state" cssClass="error" /></td>
        </tr>
        <tr>
            <td><label>Country</label>  </td>
            <td><springForm:input path="employeeDetail.country" /></td>
            <td><springForm:errors path="employeeDetail.country" cssClass="error" /></td>
        </tr>
        <tr>
            <td colspan="2" style="text-align: center;">
                <input type="submit" value="Add Info" />
            </td>
        </tr>
    </table>
</springForm:form>

In this jsp First Name, Last Name and email belong to a model class i.e Employee. Last all attributes like Street, City etc belong to EmployeeDetails model class.

public class Employee {

private Long empId;

@Size(min=2, max=30)
private String firstName;

@Size(min=2, max=30)
private String lastName;

@NotEmpty @Email
private String email;

@DateTimeFormat(pattern="MM/dd/yyyy")
@NotNull @Past
private Date doj;

@Phone
private String phone;

private EmployeeDetail employeeDetail;

Getter/Setter
}

EmployeeDetail:

public class EmployeeDetail {

@Size(min=2, max=30)
private String street;

@Size(min=2, max=30)
private String city;

@Size(min=2, max=30)
private String state;

@Size(min=2, max=30)
private String country;
//Getter/Setter
}

I am performing validation on both model inside a single form. My Cotroller mapping is:

@RequestMapping(value = "/saveEmpInfo", method = RequestMethod.POST)
public String saveEmployee(@Valid Employee employee,
        @Valid EmployeeDetail employeeDetail, BindingResult bindingResult) {
    employee.setEmployeeDetail(employeeDetail);
    if (bindingResult.hasErrors()) {
        System.out.println("Returning addInfo.jsp page");
        return "addInfo";
    }
    //future logic
}

But It's not working getting Bad Request.enter image description here


Solution

  • To make this work you should annotate employeeDetails with @Valid in your Employee.class like this:

    @Valid
    private EmployeeDetail employeeDetail;
    

    And now in controller you should use just Employee object like this:

    public String saveEmployee(@Valid Employee employee, BindingResult bindingResult) {