Sorry I don't have a working example, since in my software I try to avoid such a metaclasses that are diffcult to mantain. I prefer using strongly typed classses+some dependency injection+interfaces to make they "open" to new types.
However the basic idea is simple, at leasdt for the server side validation (it is better you start from there, since it is easier).
You implement a custom validationa attribute that just specified the name of another property whose value would be the actual validation attribute to apply. Let make an examle
[Broker("ValidationType", ValidationArguments)]
public string UserValue{get; set;)
public Type ValidationType {get; set;}
public object[] ValidationArguments {get; set}
Now since the property ValidationType can accept a Type you can give it the type of a validation attribute as value:
x.ValidationType=typeof(RequiredAttribute);....
x.ValidationArgument = new object[0];
Now when you write the code of the BrokerAttribute. In its IsValid method. you just access the ValidationType property that is passed as parameter of the BrokerAttribute, and via reflection you invoke its IsValid Method. You use the ValidationArgument to
pass some parameters to the valòidation attribute
This way you can implement a "variable" validation, whose validation rules are specified in the properties of the classes of your list.
francesco ab...
All-Star
20912 Points
3279 Posts
Re: Custom Validation on Collection of heterogeneous items
May 09, 2012 02:02 PM|LINK
Sorry I don't have a working example, since in my software I try to avoid such a metaclasses that are diffcult to mantain. I prefer using strongly typed classses+some dependency injection+interfaces to make they "open" to new types.
However the basic idea is simple, at leasdt for the server side validation (it is better you start from there, since it is easier).
You implement a custom validationa attribute that just specified the name of another property whose value would be the actual validation attribute to apply. Let make an examle
[Broker("ValidationType", ValidationArguments)]
public string UserValue{get; set;)
public Type ValidationType {get; set;}
public object[] ValidationArguments {get; set}
Now since the property ValidationType can accept a Type you can give it the type of a validation attribute as value:
x.ValidationType=typeof(RequiredAttribute);....
x.ValidationArgument = new object[0];
Now when you write the code of the BrokerAttribute. In its IsValid method. you just access the ValidationType property that is passed as parameter of the BrokerAttribute, and via reflection you invoke its IsValid Method. You use the ValidationArgument to pass some parameters to the valòidation attribute
This way you can implement a "variable" validation, whose validation rules are specified in the properties of the classes of your list.
Mvc Controls Toolkit | Data Moving Plug-in Videos