0

Hibernate Validator 4.3.1 に問題があります。

問題は、バリデータが空で @NotEmpty アノテーションを持たないフィールドを検証していることです。

個人の Web フォームが送信され、住所、電話、ファックス、および 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 + "]";
    }

}

編集 - 問題は解決しました。

問題は"^"表現にあった。"[^|]"代わりに使用する必要があります。

4

2 に答える 2

2

あなたの質問を理解するのは難しかったですが、電話が空のときに検証エラーが発生すると言っていると思います。

これは、最小値が定義された @Length アノテーションがあるためです。

于 2013-01-04T11:18:49.107 に答える
0

null と空の場合に「true」を返すには、カスタム制約注釈を作成する必要があると思います。あなたが抱えているかもしれない問題は、ユーザーが入力ボックスをクリックしても何も入力しない場合、フィールドは自動的に空に設定されますが、フィールドとの対話がなければnullが返されます(はずです)。

非常に単純な tut >>> http://codetutr.com/2013/05/29/custom-spring-mvc-validation-annotations/と、Springs リファレンス ドキュメントがあります >>> 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

于 2013-10-17T14:23:58.447 に答える