I created a custom validation to <form:select> that populate country list.
Customer.jsp
Country:
<form:select path="country" items="${countries}" />
<form:errors path="country" cssClass="error"/>
FomeController.java
@RequestMapping(value = "/customer", method = RequestMethod.POST)
public String prosCustomer(Model model,
@Valid @ModelAttribute("defaultcustomer") Customer customer,
BindingResult result
) {
CustomerValidator vali = new CustomerValidator();
vali.validate(customer, result);
if (result.hasErrors()) {
return "form/customer";
} else {
...
}
}
CustomValidator.java
public class CustomerValidator implements Validator {
@Override
public boolean supports(Class<?> type) {
return Customer.class.equals(type);
}
@Override
public void validate(Object target, Errors errors) {
Customer customer = (Customer) target;
int countyid=Integer.parseInt(customer.getCountry().getCountry());
if (countyid==0) {
errors.rejectValue("country", "This value is cannot be empty");
}
}
}
Customer.java
private Country country;
Validation is working perfectly fine. But the problem is that the validation method has attached another message too.
Please tell me how to correct this message.