Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
635 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...