【问题标题】:Hibernate validator validate empty field where is not @NotEmptyHibernate 验证器验证不是 @NotEmpty 的空字段
【发布时间】:2013-01-04 11:03:40
【问题描述】:

我对 Hibernate Validator 4.3.1 有疑问。

问题是,验证器正在验证字段为空且没有 @NotEmpty 注释。

当人的 web 表单被提交并且地址、电话、传真和网页未设置时,将引发验证错误。我认为这是错误的,因为没有 @NotEmpty 注释。我想跳过没有 @NotEmpty 注释的字段。

谁能解释问题出在哪里?谢谢。

public class Person{

    private String name;
    private String notifiedBodyCode;
    private Address address;
    private String webpage;
    private String phone;
    private String fax;
    private String email;

    @Column(name = "name", length = 100)
    @NotEmpty(message = "{NotEmpty.NotifiedBody.name}")
    @Length(max = 100)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @NotEmpty
    @Column(name = "notified_body_code", length = 25)
    @Length(max = 25)
    public String getNotifiedBodyCode() {
        return notifiedBodyCode;
    }

    public void setNotifiedBodyCode(String notifiedBodyCode) {
        this.notifiedBodyCode = notifiedBodyCode;
    }

    @Valid
    @ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    @Pattern(regexp = "^(https?://)?[a-z_0-9\\-\\.]+\\.[a-z]{2,4}.*$")
    @Column(name = "web", length = 50)
    public String getWebpage() {
        return webpage;
    }

    public void setWebpage(String webpage) {
        this.webpage = webpage;
    }

    @Length(min = 9, max = 20)
    @Pattern(regexp ="[0-9\\+\\s]")
    @Column(name = "phone", length = 20)
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Length(min = 9, max = 20)
    @Column(name = "fax", length = 20)
    public String getFax() {
        return fax;
    }

    public void setFax(String fax) {
        this.fax = fax;
    }

}

地址

public class Address{


    private String city;
    private String street;
    private String zip;
    private Country country;


    @Length(max = 50)
    @Column( name = "city", length= 50)
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Length(max = 100)
    @Column( name = "street", length= 100)
    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @Length(min = 5, max = 6)
    @Pattern(regexp = "^[\\d\\s]$")
    @Column(name = "zip", length = 6)
    public String getZip() {
        return (zip == null ? "" : zip);
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "country_id")
    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

}

国家

public class Country{

    private String countryName;

    @NotEmpty
    @Length(max = 45)
    @Column(name = "country_name", length = 45)
    public String getCountryName() {
        return countryName;
    }

    public void setCountryName(String countryName) {
        this.countryName = countryName;
    }

    @Override
    public String toString() {
        return "Country [id=" + id + ", countryName=" + countryName + "]";
    }

}

编辑 - 问题已解决。

问题出在"^" 表达式中。我必须改用"[^|]"

【问题讨论】:

  • 澄清你的答案。您实际上在字段上有约束注释。没有任何注释就不会发生验证,但在您的情况下,您还使用了 @Pattern 注释,该注释对空字符串失败。

标签: java spring validation spring-mvc hibernate-validator


【解决方案1】:

很难理解您的问题,但我假设您是说手机为空时出现验证错误。

那是因为您有一个带有已定义最小值的 @Length 注释。

【讨论】:

  • 但我想跳过没有@NotEmpty 注释且不是字段的验证字段。如果给定字段包含任何字符,则将被验证。
  • @Length 如果您的字段为 NULL,则返回“有效”,因此在设置它的代码中,您的“电话”可能被设置为空字符串而不是 NULL。从“org.hibernate.validator.constraints.impl.LenghtValidtor.class”中剥离: public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { if ( value == null ) { return true; } int 长度 = value.length();返回长度 >= 最小 && 长度
  • 感谢您的回答。但我不知道为什么抛出错误“必须匹配”^(https?://)?[a-z_0-9\-\.]+\.[a-z]{2,4}.*$" 长度必须是在 9 到 20 和其他之间”当输入为空时。你知道问题出在哪里吗?
  • “空”和“空”是有区别的。如果您的字段为“空”,即它们是“”或“new String()”,它们将无法通过验证,因为它们与您的“@Length”或“@Pattern”验证器不匹配。但是,如果您的字段为 NULL,那么只要您没有“@NotNull”注释,它们就会通过验证。所以正如我之前评论过的,问题是你的字段被设置为空字符串而不是空对象
  • 我做了,但还是一样。
【解决方案2】:

我相信您需要创建一个自定义约束注释以在 null 和空时返回“true”。您可能遇到的问题是,当用户单击输入框但未输入任何内容时,该字段会自动将自身设置为空,而没有与该字段进行任何交互将(应该)返回 null。

一个非常简单的 tut >>> http://codetutr.com/2013/05/29/custom-spring-mvc-validation-annotations/ 然后你有弹簧参考文档 >>> http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html,还有,http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html/validator-usingvalidator.html

【讨论】:

    猜你喜欢
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-02-10
    • 2012-07-26
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多