4

I'm working on a Spring MVC application and have a question about spring validation. First, I have this action in my controller:

@RequestMapping(value = "/create", method = RequestMethod.POST, produces = "application/json")
public @ResponseBody
Employee employeeCreate(@Valid @RequestBody EmployeeModelCreate objModel) throws Exception {
  ...
}

And this is my EmployeeModelCreate class:

public class EmployeeModelCreate implements Serializable {
    ...
    @NotBlank(message = "...")
    private String password;

    @NotBlank(message = "...")
    private String confirmPassword;
    ...

    //Setters and Getters
}

Now, I want to have validation for comparing password and confirmPassword. This validation should be check equality of these two fields. How can I do that? Any help would be greatly appreciated.

hamed
  • 7,939
  • 15
  • 60
  • 114

1 Answers1

5

You could make a custom validation class, for this you need to implement the Validator interface, then you can use it manually, or bind it to the Spring MVC controller, using the @InitBinder annotation.

This question may contain other useful information.

Community
  • 1
  • 1
meskobalazs
  • 15,741
  • 2
  • 40
  • 63
  • Thank your for your replay @meskobalazs. Your suggested link is very good and helped me a lot. Thanks again. – hamed Apr 29 '15 at 05:33