Question

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;
    }
}
Était-ce utile?

La solution

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.

Autres conseils

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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top