I have implemented custom validation for my JSP forms and when there are errors the form is returned with my errors bound to the BindingResult. My form looks like:
<form:form name="createCustomer" action="/practicemvc/customers/create/" method="POST" modelAttribute="customerBean">
<form:errors />
<label for="customerName">Name</label>
<input type="text" name="name" id="customerName" value="${customerBean.name}" />
<form:errors path="name" />
The errors are displayed through:
<form:errors />
But, they are not being displayed through:
<form:errors path="name" />
My errors are merged to the BindingResult with:
for(ErrorType errorType: validationResult.getErrors()) {
bindingResult.addError(new ObjectError(errorType.getProperty(),
new String[]{errorType.getErrorCode()}, null, null));
}
getProperty() will return “name”, and getErrorCode() returns the “INVALID_EMAIL”. “INVALID_EMAIL” translates to “Your email is invalid” through my messageSource bean. If I look at the contents of the errors in my BindingResult, everything seems ok, but they are not outputting as expected in my JSP. Any ideas?
Thanks, B
As far as I remember, to associate error message with a field you need a
FieldError, whereobjectNameiscustomerBeanandfieldis the field name.