简体   繁体   English

Java Spring:如何仅验证实体中的特定字段

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

I'm new in Spring and I have a problem with form validation. 我是Spring的新手,但是表单验证存在问题。 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: 例如,如果我的UserEntity具有以下字段:

@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'. 但是,当我编辑了用户表单时,我只想编辑和验证“姓名”,“电子邮件”和“电话”,而不希望更改“ userlogin”和“ userpass”。 But edit form won't pass successfully, because BindingResult validating all fields. 但是编辑表单不会成功通过,因为BindingResult会验证所有字段。 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'. result.hasError()始终返回true,因为它还会验证'userlogin'和'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. 推土机这样的东西可能会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Java 仅查询 MongoDB 中的特定字段? - How to query only the specific fields in MongoDB with Java? 在spring boot中仅从api返回模型的特定字段 - Return only specific fields of model from api in spring boot 如何在Spring Boot中使用Java从JSON响应中提取特定部分,并更改JSON结构中的字段名称(如本例所示)? - How to extract a specific part from JSON response with changing fields names in JSON structure (as in this example) with Java in Spring Boot? MongoDB:如何使用 Java(使用或不使用 Spring)将带有别名的一个查询的特定字段添加到另一个查询的结果? - MongoDB: how to add the specific fields from one query with alias to result of another query using Java (with or without Spring)? 如何只返回Spring数据MongoDB中查询的特定字段? - How to return only specific fields for a query in Spring Data MongoDB? 如何使用spring CrudRepository仅选择特定字段? - How to select only specific fields using spring CrudRepository? 如何在查询 Java Spring Mongo Repository 中返回特定字段的列表? - How to return list of specific fields in query Java Spring Mongo Repository? 如何在 Spring Boot 控制台应用程序中验证实体? - How to validate entity in Spring Boot console application? Java + Spring-如何验证枚举 - Java + Spring - How to validate an enum 如何获取仅包含Java中特定字段的对象列表? - How to get a list of objects contained only specific fields in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM