문제

Can you not validate static inner classes using hibernate validation? I have the following form:

public class Thing {
    @NotNull // WORKS!
    private String message;
    private someClass obj1;
    private someOtherClass obj2;
    public static class someClass 
    {
        @NotNull //DOES NOT WORK
        private String someField;
    }
    public static class someOtherClass
    {
        @NotNull //Does NOT WORK
        private String someOtherField;
    }
}
도움이 되었습니까?

해결책

I got it, you need to mark @Valid on the instances of the someClass and someOtherClass. This fixed the issue for me. Looks like the @Valid annotation I had on my controller for my Thing object wasn't applying recursively to the state of its nested objects.

다른 팁

You can use @Valid on the address property in a combination of other constraints inside Address class. A valid example would be:

public class Person {
   @NotEmpty
   private String fullName;
   @Email
   private String email; 
   @Pattern (regexp = "[0-9]+")
   private String telNo; 
   @NotNull
   @Valid
   private Address address;
}

class Address {
   @NotEmpty
   private String houseNumber; 
   @NotEmpty
   private String streetName; 
   private String province; 
   private String country;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top