We have Generics Property
public class BE
{
private List<Admin_Fee> _Admin_Fee = new List<Admin_Fee>();
[StringLengthValidator(3,
MessageTemplate = "Fund City Can't be more than 3 Chars")]
public MyProperty<string> FUND_CITY { get; set; }
public MyProperty<int> SomeOtherProperty { get; set; }
public List<MyPropertyBase> MyDataPoints { get; set; }
}
I want to put StingLengthValidator using VAB on Generic Property, and getting error that :
Value is not expected type
Can some one help?
The reason why you are getting an error is fairly straight forward: you are trying to use a
StringLengthValidatoragainst a type that isn’t a string (it is actuallyMyProperty<string>).The question is what to do to validate the property? It’s tricky because the design doesn’t really fit well within the Validation Application Block design.
Typically you would just apply an
ObjectValidatorto validate theMyPropertyclass but that doesn’t really fit in this case since it looks like you aim to useMyPropertyto hold various values each with different rules so you can’t really apply validator attributes toMyProperty.I think you can achieve what you want with custom validators. I’m thinking you can wrap existing validators inside your custom validator.
Here I’m assuming
MyPropertylooks something like:Then you can create a custom validator MyPropertyValidator:
Then you could annotate your class like so:
I haven’t tested it extensively but it seems to work. It does have a few drawbacks: