I am implementing a sample Spring MVC Form with Form Validation. I have a complex type Address
as bean property for Student
form bean. And I have added form validation @NotEmpty
for Address
bean properties. But the same is not reflecting in the UI. But form validation works for other primitive types of Student
form bean.
So, Validation works perfectly for Student
form bean but not for nested complex types like Address
within Student
form bean.
I am trying understand the reason and a fix.
Spring version 4.0+. Hibernate Validator api:5.2.4
Student
POJO:
package com.xyz.form.beans;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.Past;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
import com.xyz.validators.DateNotEmpty;
import com.xyz.validators.ListNotEmpty;
public class Student {
@Size(min = 2, max = 30)
private String firstName;
@Size(min = 2, max = 30)
private String lastName;
@NotEmpty
private String gender;
@DateNotEmpty
@Past
private Date DOB;
private String email;
private String mobileNumber;
@ListNotEmpty
private List<String> courses;
private Address address;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getDOB() {
return DOB;
}
public void setDOB(Date dOB) {
DOB = dOB;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMobileNumber() {
return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public List<String> getCourses() {
return courses;
}
public void setCourses(List<String> courses) {
this.courses = courses;
}
}
Address
POJO:
package com.xyz.form.beans; import org.hibernate.validator.constraints.NotEmpty; import com.xyz.validators.LongNotEmpty; public class Address { @NotEmpty private String houseNo; @NotEmpty private String street; @NotEmpty private String area; @NotEmpty private String city; @LongNotEmpty private Long pin; public String getHouseNo() { return houseNo; } public void setHouseNo(String houseNo) { this.houseNo = houseNo; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public Long getPin() { return pin; } public void setPin(Long pin) { this.pin = pin; } }
Student
Controller:
@RequestMapping(value = "/newStudentDetails.do", method = RequestMethod.POST) public ModelAndView newStudentDetails( @Valid @ModelAttribute("student") com.xyz.form.beans.Student studentFormBean, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return new ModelAndView("newStudentPage"); } Student studentDto = new Student(); studentDto.setFirstName(studentFormBean.getFirstName()); studentDto.setLastName(studentFormBean.getLastName()); studentDto.setGender(studentFormBean.getGender()); studentDto.setDOB(new Date(studentFormBean.getDOB().getTime())); studentDto.setEmail(studentFormBean.getEmail()); studentDto.setMobileNumber(studentFormBean.getMobileNumber()); StringBuilder sb = new StringBuilder(); sb.append(studentFormBean.getAddress().getHouseNo() + ", "); sb.append(studentFormBean.getAddress().getStreet() + ", "); sb.append(studentFormBean.getAddress().getArea() + ", "); sb.append(studentFormBean.getAddress().getCity() + "-"); sb.append(studentFormBean.getAddress().getPin()); studentDto.setAddress(sb.toString()); studentDto.setCourses(studentFormBean.getCourses()); studentDao.createStudent(studentDto); ModelAndView mav = new ModelAndView("newStudentSuccess"); return mav; }
Thanks, Viswanath
@Valid
annotation as expressed in the JSR-303 spec. So put@Valid
on theAddress
field inStudent
. – M. Deinum