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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With