Validation Application Block Question

Topics: Building and extending application blocks, Validation Application Block
Jul 23, 2008 at 8:19 PM
In my framework that my team and I are constructing, I have a contract similiar to the following: 

public class Foo
    [StringLengthValidator(1, 20)]
    public string CustomerName { get; set; }

    [StringLengthValidator(1, 100)]
    public List<string> ProductNames {get; set; }

I'd like to run this contract through the VAB. What I'm trying to code is that each string in the ProductNames list needs to have the [StringLengthValidator(1, 100)] valdiator applied. Is this the correct atttribute-based syntax? If not, then does the VAB have functionality for this requirement? Do I need to create a custom validator/attribute?

Anyway help would be greatly appreciated. Thanks!
Jul 23, 2008 at 9:10 PM

The validation specified for the ProductNames property applies to the List itself, not the elements. You can use the ObjectCollectionValidator to validate elements, but you need to specify the validation for the elements on the element type itself (string in this case, which you can only do through configuration).

Jul 24, 2008 at 1:50 PM
There's no way of performing validation of element types in a list/collection without using a config setup?
Jul 24, 2008 at 2:00 PM
It's not like that. You can use any validation on collection elements, but the validation rules must be specified on the element type instead of the collection as you originally requested. Since you cannot add new attributes to the string class, you can only use configuration to specify validation in your example (and in any case where you don't own the element type.)

Jul 24, 2008 at 3:06 PM
I follow. Since using a config is out of the question for my app, I need to build a custom validator that takes in a list/enumerable and apply a specified validator to each element. Thanks!