1

このコードを実行しています。1 つのフィールドに検証注釈を追加すると、違反が正しく検出されます。しかし、複数のフィールドに注釈を追加すると、違反はまったく検出されません。

public class Address implements Serializable {

    private static final long serialVersionUID = 225035939665473333L;

    @NotNull
    @NotEmpty
    @Size(min = 4, max = 50, message="test")
    private String Name;

    @NotNull
    @NotEmpty
    private String AddressLine1;

    private String AddressLine2;
    private String PostalCode;
    private String Province;

    @NotNull
    @NotEmpty
    private String Country;

    //getter and setters below this line
}

私はこのコードを実行しています:

ValidatorFactory factory = Validation.byDefaultProvider().configure().buildValidatorFactory();
Validator validator = factory.getValidator();
Address a = new Address();
a.setName("a");

Set<ConstraintViolation<Address>> violations = validator.validate(a);

Window.alert(String.valueOf(violations.size()));
for(ConstraintViolation<Address> violation : violations){
    Window.alert(violation.getMessage());
}

3 つのエラーを検出する必要がある場合でも、これによりゼロのエラーが検出されます。AddressLine1、Country は null であってはなりません。名前は短すぎる。クラスを以下のコードに変更すると、名前が短すぎることがわかり、エラー メッセージ "test" が表示されます。

public class Address implements Serializable {

    private static final long serialVersionUID = 225035939665473333L;

    @NotNull
    @NotEmpty
    @Size(min = 4, max = 50, message="test")
    private String Name;

    private String AddressLine1;

    private String AddressLine2;
    private String PostalCode;
    private String Province;

    private String Country;

    //getter and setters below this line
}
4

0 に答える 0