Hi in my project when I am trying to validate my form its not showing any error messages even if validation fails (Even Form is not submitted and enters into validation fail block)
Here is my code
/****************** Post Method *************/
@RequestMapping(value="/property", method = RequestMethod.POST)
public String saveOrUpdateProperty(@ModelAttribute("property") Property property,
BindingResult result,
Model model,
HttpServletRequest request) throws Exception {
try {
if(validateFormData(property, result)) {
model.addAttribute("property", new Property());
return "property/postProperty";
}
}
/********* Validate Block *************/
private boolean validateFormData(Property property, BindingResult result) throws DaoException {
if (property.getPropertyType() == null || property.getPropertyType().equals("")) {
result.rejectValue("propertyType", "Cannot Be Empty !", "Cannot Be Empty !");
}
if (property.getTitle() == null || property.getTitle().equals("")) {
result.rejectValue("title", "Cannot Be Empty !", "Cannot Be Empty !");
}
return (result.hasFieldErrors() || result.hasErrors());
}
But when i debug i can see below one
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'property' on field 'title': rejected value [null]; codes [Cannot Be Empty !.property.title,Cannot Be Empty !.title,Cannot Be Empty !.java.lang.String,Cannot Be Empty !]; arguments []; default message [Cannot Be Empty !]
and this is how i am displaying in jsp file
<div class="control-group">
<div class="controls">
<label class="control-label"><span class="required">* </span>Property Type</label>
<div class="controls">
<form:input path="title" placeholder="Pin Code" cssClass="form-control border-radius-4 textField"/>
<form:errors path="title" style="color:red;"/>
</div>
</div>
</div>
Event though when i see the below one when i debug (1 Error its correct)
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Why it is not displayed in jsp can any one hep me?
I think that you cannot see anything because on the second line below you destroy your model (your validation error included) and create a new one.
if(validateFormData(property, result)) {
model.addAttribute("property", new Property()); // <------
return "property/postProperty";
Try display the property that comes in as a parameter and probably you will be able to see the validation errors.
if(validateFormData(property, result)) {
model.addAttribute("property", property);
return "property/postProperty";