Home  >  Forum  >  Spring
Post New Query

java.lang.IllegalStateException: Invalid target for Validator


joined on
June 15,2014
Asked on April 15,2015
Hi I am using spring Validator interface for validation.

public class LoginValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return false;
    }

    @Override
    public void validate(Object target, Errors errors) {
        ------------------------
        ------------------------
    }

}

and adding it in controller.

@InitBinder
public void dataBinding(WebDataBinder binder) {
    binder.addValidators(new LoginValidator());

}  

But getting exception

java.lang.IllegalStateException: Invalid target for Validator
    org.springframework.validation.DataBinder.assertValidators(DataBinder.java:516)
    org.springframework.validation.DataBinder.addValidators(DataBinder.java:527)


joined on
October 10,2013
Replied on April 15,2015

You have not implemented supports method correctly. Suppose you have JavaBean as User.java.
Then implement supports method as

    @Override
    public boolean supports(Class<?> clazz) {
    return User.class.isAssignableFrom(clazz);
    }


This defines that if the instance of class passed as an argument can be validated or not.

Write Answer



Sitewide-Feb4of4234x60





Copyright ©2017 concretepage.com, all rights reserved |Privacy Policy | Contact Us