Question: I was looking at other questions about Spring custom validators but unfortunately I could not solve my problem with the proposed answers.

My problem is the following: I have an entity (Account) and I created a custom validator (AccountValidator) which I use in a controller (RegisterController), but it is never invoked, using the default Validator.

Am I forgetting something? I attach part of the code to help understand better my problem.

**Validator:**

```
public class AccountValidator implements Validator{

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

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

    //Validation code

    }

}

```

**Controller:**

```
@Controller
@RequestMapping(value = "/register")
public class RegisterController {

    @Autowired
    private AccountValidator accountValidator;

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields("id");
        binder.setValidator(accountValidator);
    }

    @RequestMapping(method = RequestMethod.GET)
    @ModelAttribute
    public Account register(Locale currentLocale){
        Account account = new Account();
        return account;
    }

    @RequestMapping(method = RequestMethod.POST)
    public String handleRegister(@Valid @ModelAttribute Account account, BindingResult result){
        if(result.hasErrors()){
            return "/register";
        }
        return "home";
    }
}

```

I checked my debug messages in the log, and the initBinder method is being called, but the validation method is never being executed.

Can anyone help me?

Answer: 
I was facing the same issue and i fixed it by declaring the class **AccountValidator** in context xml file and using `@validated` in place of `@valid`.
After going through the source code, the only reason I can find for the `WebDataBinder` not to invoke your `Validator` is that your variable is `null`. This field

```
@Autowired
private AccountValidator accountValidator;

```

must be `null`. I don't know how you got there, Spring would complain if it couldn't autowire a field. 

---

At the moment, I can't tell you why the `Validator` isn't being called when registered with the `WebDataBinder`, but here's the workaround:

Get rid of the 

```
binder.setValidator(accountValidator);

```

and add the the `Validator` call in the handler method

```
@RequestMapping(method = RequestMethod.POST)
public String handleRegister(@Valid @ModelAttribute Account account, BindingResult result){
    accountValidator.validate(account, result);
    if(result.hasErrors()){
        return "/register";
    }
    return "home";
}

```

Spring will perform default validation (based on your validation provider, ex. Hibernate) and then you apply your custom validation.