1

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";

}

2 Answers 2

1

You should use @Validated-annotation with provided validation-group and appropriate validators. Here you see such examples : http://www.journaldev.com/2668/spring-validation-example-mvc-validator

Sign up to request clarification or add additional context in comments.

5 Comments

I change this : public String resetPass(@ModelAttribute("email") @Validated User user, BindingResult bindingResult){ .... but getting error: Neither BindingResult nor plain target object for bean name 'email' available as request attribute
please look in the link, you should to add some dependencies and provide your custom validators
I don't want to create custom validation that is whole point of asking this question.
So, you can pass "forgot password"-case
what is forgot password case?
1

Don't know if I'm understanding you right, but I kind of had a similar problem, where a User updates its credentials and there is of course the possibility, that the password-field ist empty (if the User doesn't want to (re) set it). So I simply check if the password field was empty or not (it may be a "dirty" solution, but in my case it worked (I additionally did a form validation on html-side)):

if (bindingResult.hasErrors()) {
     // ignore field password
     if (!bindingResult.hasFieldErrors("password")) {
          // do something
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.