Spring – How to use ValidationUtils in spring


I want to use ValidationUtils as follows. But I cannot instantiate errors object since Errors is an Interface. Can you tell me how I can make this working without using a validator?

        Errors errors;
        visitorDetails=new Visitor(); 
        ValidationUtils.rejectIfEmpty(errors, "VisitorInfo", "Selected Visitor Details Not Found");

Best Solution

Read this : Validation...

However you must implement the Validation interface in a class, and than use it to validate your object, and to do that you autowire validator in your controller..

This is an example:

public class PersonValidator implements Validator {

    * This Validator validates *just* Person instances
    public boolean supports(Class clazz) {
        return Person.class.equals(clazz);

    public void validate(Object obj, Errors e) {
        ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
        Person p = (Person) obj;
        if (p.getAge() < 0) {
            e.rejectValue("age", "negativevalue");
        } else if (p.getAge() > 110) {
            e.rejectValue("age", "too.darn.old");

and in your controller:


    private PersonValidator personValidator;

    protected void initBinder(final HttpServletRequest request, final ServletRequestDataBinder binder) {