Validate Collection property

Topics: Validation Application Block
Jun 8, 2010 at 5:45 AM

I have a class that has a property which is a collection of another class (public List<AddressDetails> Address { get; set; }) Can the validation framework iterate over the collection validating each AddressDetails which contains standard validation attributes.

Jun 8, 2010 at 7:44 AM

You can simply decorate that property with the  ObjectCollectionValidatorAttribute:

[ObjectCollectionValidator(typeof(AddressDetails))]
public List<AddressDetails> Address { get; set; }
When the list not only contains instances of AddressDetails, but also of sub types of AddressDetails (that have their own custom validation) you can use the default constructor of the ObjectCollectionValidatorAttribute (EntLib 5.0 only):

[ObjectCollectionValidator]
public List<AddressDetails> Address { get; set; }