Hi Mihai,
If the type of an address is known within the Address class, you could do the following:
* Add two @NotNull constraints with different messages and add them to two different validation groups:
Code:
class Address {
@NotEmpty.List({
@NotEmpty(message = "{person.address.body.notEmpty}", groups=PersonAddressChecks.class),
@NotEmpty(message = "{address.notEmpy}", groups=ContactAddressChecks.class)
})
String body;
}
* Implement a group sequence provider for the Address class (see 2.3.2.2 in our reference guide - http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-default-group-class). DefaultGroupSequenceProvider is a Hibernate-Validator specific API which allows to determine the applying default group sequence in a dynamic way:
Code:
public class AddressGroupSequenceProvider implements DefaultGroupSequenceProvider<Address> {
public List<Class<?>> getValidationGroups(Address address) {
if ( address.getType() == AddressType.PERSON ) ) {
return Arrays.asList ( PersonAddressChecks.class, Address.class );
}
else {
return Arrays.asList ( ContactAddressChecks.class, Address.class );
}
}
}
* Add the provider to your Address class:
Code:
@GroupSequenceProvider(AddressGroupSequenceProvider.class)
class Address {
//...
}
That way the registered group sequence provider will be used to determine the applying groups when validating the Address within the default group. Depending on the address type, either the PersonAddressChecks group or the ContactAddressChecks group will be validated, causing the @NotEmpty constraint with the right message to fail.
--Gunnar