Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate validation on static inner classes

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;
    }
}
like image 975
user1553248 Avatar asked May 13 '14 22:05

user1553248


2 Answers

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.

like image 73
user1553248 Avatar answered Oct 22 '22 12:10

user1553248


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;
}
like image 7
adelekeoladapo Avatar answered Oct 22 '22 11:10

adelekeoladapo