I have the following code for validation
@RequestMapping(value = "/itemValidation.json", method = RequestMethod.POST)
@ResponseBody
public ValidationResponse ajaxValidation(
@ModelAttribute(value = formName) @Valid Item item,
BindingResult result) {
ValidationResponse res = new ValidationResponse();
if (!result.hasErrors()) {
res.setStatus("SUCCESS");
} else {
res.setStatus("FAIL");
List<FieldError> allErrors = result.getFieldErrors();
List<ErrorMessage> errorMesages = new ArrayList<ErrorMessage>();
for (FieldError objectError : allErrors) {
errorMesages.add(new ErrorMessage(objectError.getField(),
objectError.getDefaultMessage()));
}
res.setErrorMessageList(errorMesages);
}
return res;
}
Upon validation, there are three elements that do not satisfy the constraints as shown below:
The problem is on the JSP only the last two of the errors are shown. The error with fieldName
: itemPK.name
is not shown.
I use the below code to show the errors:
<span class="help-inline"><form:errors path="${name}" /></span>
My generated input elements in a sequence:
<input id="itemPK.name_id" name="itemPK.name" type="text" value="">
<input id="price_id" name="price" type="number" value="">
<input id="point_id" name="point" type="number" value="">
Not sure what went wrong, hope anyone can shed some light on this.
I found out what the problem was . There's another layer where an ajax response method suppose to append all the error messages to the appropriate fields. The first error was never shown due to the jQuery selector unable to locate element with name itemPK.name
, the fix was to change it to itemPK\\.name
.