简体   繁体   中英

Java Spring: how to validate only specific fields from my entity

I'm new in Spring and I have a problem with form validation. In my user edit form I want to validate only specific fields, not all fields annotated in my entity. For example, if I have my UserEntity with fields:

@Entity
@Table(name = "users")
public class UserEntity {

@Id
@Column(name = "user_id")
@GeneratedValue
public int user_id;

@NotEmpty
@Column(name = "userlogin")
public String userlogin;

@NotEmpty
@Column(name = "userpass")
public String userpass;

@NotEmpty
@Column(name = "name")
public String name;

@Email
@NotEmpty
@Column(name = "email")
public String email;

@NumberFormat(style = Style.NUMBER)
@NotEmpty
@Column(name = "phone")
public String phone;

and when I have register form, I need to validate all fields, and that's working fine. But when I have edit user form, I want to edit and validate only 'name', 'email' and 'phone', I don't want to change 'userlogin' and 'userpass'. But edit form won't pass successfully, because BindingResult validating all fields. Here's my edit form:

 <springForm:form action="/mywebapp/user/edit" commandName="user" method="POST">
    <table>     
        <tr>
            <td>Name:</td>
            <td><springForm:input path="name" value="${user.name}" /></td>
            <td><springForm:errors path="name" cssClass="error" /></td>
        </tr>
        <tr>
            <td>E-mail:</td>
            <td><springForm:input path="email" value="${user.email }" /></td>
            <td><springForm:errors path="email" cssClass="error" /></td>
        </tr>
        <tr>
            <td>Phone:</td>
            <td><springForm:input path="phone" value="${user.phone}" /></td>
            <td><springForm:errors path="phone" cssClass="error" /></td>

        </tr>
        <tr>
            <td><input type="submit" value="Edit" /></td>
        </tr>
    </table>
</springForm:form>

Here is my controller method:

 @RequestMapping(value="user/edit", method=RequestMethod.POST)
 public String doUserEdit(@Valid @ModelAttribute("user") UserEntity user, BindingResult result, Principal principal) {

    if(result.hasErrors()) {
        return "user/edit";
    }
    UserEntity u = this.userService.getUser(principal.getName());


    this.userService.editUser(u.getUser_id(), user);

    return "redirect:/user";
}

result.hasError() always return true, because it validating also 'userlogin' and 'userpass'.

How to ignore other fields in edit form and validate only that fields that I want to?

I usually create a separate form class which is only suited for form submission processing and put all the necessary validation there:

public class UserUpdateForm {
    @NotEmpty
    private String name;
    @Email
    private String email;
    @NotEmpty
    @NumberFormat(style = Style.NUMBER)
    private String phone;

    //Getters and setters here
} 

The idea is that you untie your model class from representations (form) classes. The only downside to that is that you'll have to handle transformations between the form objects and model objects somehow. Something like dozer might help though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM