2013-01-04 48 views
0

我有Hibernate Validator 4.3.1的问题。Hibernate验证器验证空字段不是@NotEmpty

问题是验证器验证字段为空且没有@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 + "]"; 
    } 

} 

编辑 - 问题解决了。

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

+1

澄清你的答案。您实际上在字段上有约束注释。如果没有任何注释,则不会进行验证,但在您的情况下,您还使用了空字符串失败的@Pattern注释。 – Hardy

回答

2

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

这将是因为你有一个@Length注解和一个已定义的最小值。

+0

但我想跳过验证字段没有@NotEmpty注释,而不是字段。如果给定的字段包含任何字符,则会被验证。 –

+0

@Length确实返回“有效”,如果你的字段为NULL,所以也许你的“手机”被设置为空字符串,而不是在设置它的代码中为NULL。从 “org.hibernate.validator.constraints.impl.LenghtValidtor.class” 地带:公共布尔参考isValid(字符串值,ConstraintValidatorContext constraintValidatorContext){ \t \t如果(价值== NULL){ \t \t \t回归真实; \t \t} \t \t int length = value.length(); \t \t返回长度> = min && length <= max; \t} – beder

+0

谢谢你的回答。但我不知道为什么抛出的错误“必须匹配”^(https?://)?[a-z_0-9 \ - \。] + \。[az] {2,4}。* $“ 长度必须在9到20之间 和其他“当输入空时。哟,你有什么想法是什么问题? –

0

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

一个非常简单的啧啧>>>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