In my spring boot app. I have user class:
public class User {
private Long id;
@NotEmpty(message="Field is required.")
@Size(max=50, message="Field cannot exceed 50 characters in length.")
private String username;
@NotEmpty(message="Field is required.")
@Size(max=50, message="Field cannot exceed 50 characters in length.")
private String name;
@NotEmpty(message="Field is required.")
@Email(message="Please enter valid email address.")
private String email;
.... getter and setter here....
}
I created user registration form where I am validating name, username and email.
Like this:
@RequestMapping(value="/user/register", method= RequestMethod.GET)
public String createUserForm(User user){
return "user/create";
}
@RequestMapping(value="/user/register", method= RequestMethod.POST)
public String createUser(@Valid User user, BindingResult bindingResult){
if (bindingResult.hasErrors()) {
return "users/create";
}
//Here code to save user
return "users/createdSuccess";
}
Now I wanted to create forgot the password page for users. Forget password page only contains email address. But If I use validate user model it always gives error as there is annotion to name and username attributes are added.
How I can ignore name and username annotation for forget password page and validate only email address.
@RequestMapping(value="/account/forgot", method= RequestMethod.GET)
public String forgotPassForm(User user){
return "users/forgotPassword";
}
@RequestMapping(value="/account/forgot", method= RequestMethod.POST)
public String resetPass(@Valid User user, BindingResult bindingResult){
if (bindingResult.hasErrors()) {
return "users/forgotPassword";
}
return "users/passwordEmailSent";
}