2

Hi this is my controller

@RequestMapping(value="/home", method = RequestMethod.POST)
public String homeSubmit(@Valid LoginForm loginForm , BindingResult bindResult){

    if(bindResult.hasErrors() ||  bindResult.hasFieldErrors() ){
        return "home";
    }else{
        return "Success";
    }
}

@InitBinder("loginForm")
protected void initBinder(WebDataBinder binder) {
    binder.setValidator(new LoginFormValidator());
}

following is my validator

public class LoginFormValidator implements Validator {

    public boolean supports(Class<?> classz) {
        return LoginForm.class.equals(classz); 
    }

    public void validate(Object target, Errors arg1) {
        System.out.println("inside validate method validator");
        LoginForm loginForm = (LoginForm) target;
        if(! loginForm.getUserName().equalsIgnoreCase("xxx")  ){
            arg1.rejectValue("userName","Invalid Data");
        }
        if(! loginForm.getPassword().equalsIgnoreCase("yyy")){
            arg1.rejectValue("password","Invalid Data");
        }
    }
}

Problem is my validator is not all getting called.evertime the conditions bindResult.hasErrors() || bindResult.hasFieldErrors() is false.I also tried debugging..It is not all getting called

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Renganathan V
  • 413
  • 1
  • 9
  • 27
  • 1
    Try adding `@ModelAttrubute("loginForm")` right before `@Valid` and let me know what happens – geoand May 13 '14 at 09:14
  • Binding and validation are two different things. If binding fails then validation doesn't make sense. – a better oliver May 13 '14 at 09:28
  • Isn't the `initBinder` method called or is your validator not called? I suspect the second and not the first, if so please modify the title of the question. – M. Deinum May 13 '14 at 10:56

1 Answers1

3

One way to do this is to instantiate Validator as service, and inject into to your controller. Annotate @Validated on your model.

@Autowired
@Qualifier("loginFormValidator")
private Validator validator;

@RequestMapping(value="/home", method = RequestMethod.POST)
public String homeSubmit(@Validated LoginForm loginForm , BindingResult bindResult){

    if(bindResult.hasErrors() ||  bindResult.hasFieldErrors() ){
        return "home";
    }else{
        return "Success";
    }
}

Instantiate your validator as service:

@Service("loginFormValidator")
public class LoginFormValidator implements Validator {

    public boolean supports(Class<?> classz) {
        return LoginForm.class.equals(classz); 
    }

    public void validate(Object target, Errors arg1) {
        System.out.println("inside validate method validator");
        LoginForm loginForm = (LoginForm) target;
        if(! loginForm.getUserName().equalsIgnoreCase("xxx")  ){
            arg1.rejectValue("userName","Invalid Data");
        }
        if(! loginForm.getPassword().equalsIgnoreCase("yyy")){
            arg1.rejectValue("password","Invalid Data");
        }
    }
}
Faizal Sidek
  • 166
  • 1
  • 7