Hibernate验证器验证空字段,而不是@NotEmpty

时间:2013-01-04 11:03:40

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

我遇到Hibernate Validator 4.3.1的问题。

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

当提交人的网页表单并且未设置地址,电话,传真和网页时,将引发验证错误。我认为这是错误的,因为没有@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 + "]";
    }

}

编辑 - 问题已解决。

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

2 个答案:

答案 0 :(得分:2)

很难理解你的问题,但我认为你说当手机电量耗尽时你会收到验证错误。

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

答案 1 :(得分:0)

我相信你需要创建一个自定义约束注释,以便在null和empty上返回“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