Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

validation - Validate partial Modal using Spring @Valid annotation

I have a User Modal

public class RegisterUser {

    @Size(min = 2, max = 30)
    private String fname;

    @Size(min = 2, max = 30)
    private String lname;

    @NotEmpty
    @Size(min = 6, max = 15)
    private String password;

    ....

    @NotEmpty
    private String publicProfile;

     ... getters and setters

}

1) I want to use this modal during registration action (fname, lname, password etc but without publicProfile field)

2) I want to use this modal during myprofile action (all fields except password)

My action for register:

@RequestMapping(value = "/register", method = RequestMethod.POST)
public String submitRegisterForm(
        @Valid RegisterUser registerUser,
        BindingResult result,
        Model m) {
    ....
  }

Here I don't intend to provide 'publicprofile' on jsp and therefore do not want to validate this field although my Modal has @NotEmpty annotation

My action for myprofile

@RequestMapping(value = "/myprofile", method = RequestMethod.POST)
public String submitMyprofileForm(
    @Valid RegisterUser registerUser,
        BindingResult result,
        Model m) {
    ....
}

Here I don't intend to provide 'password' field on jsp and therefore do not want to validate this field although my Modal has @NotEmpty and @Size(min = 6, max = 15) annotation

My question is how can I achieve this ? Is there any way where I can say in this modal for this action validate only mentioned fields?

Thanks in advance Manisha

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use Validation Groups (for different scenarios) and Spring's @Validated annotation to specify which group you want to use


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...