I am about to setup form validation in Spring 3.1. I am using Annotations to validate my Model, like this:
Model:
@Column(name = "mailAddress", nullable = false)
@Email
private String mailAddress;
@Column(name = "school", nullable = false)
@NotBlank
@Size(min = 3, max = 100)
private String school;
Controller:
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addBooking(
@ModelAttribute("new-booking") @Valid Booking booking,
BindingResult result, Map<String, Object> model) {
if (result.hasErrors()) {
return "booking";
}
return "success";
}
The Problem is, it validates the school but not the mailAddress. If you enter an empty mailAddress it will accept it.
I found the Issue. The Email Validator will accept Blank Emails. To fix this you only need to add a
@NotBlankto it. Actually I thought the nullable = false would be enought, but wasn’t.