At the moment the xVal.RuleProviders.DataAnnotationsRuleProvider
only looks at properties defined on the model class itself. You can see this in the method GetRulesFromProperty
in the rule provider base class PropertyAttributeRuleProviderBase
:
protected virtual IEnumerable<Rule> GetRulesFromProperty(
PropertyDescriptor propertyDescriptor)
{
return from att in propertyDescriptor.Attributes.OfType<TAttribute>()
from validationRule in MakeValidationRulesFromAttribute(att)
where validationRule != null
select validationRule;
}
The propertyDescriptor
parameter represents a property in your model class and its Attributes
property represents only the attributes defined directly on the property itself.
However, you could of course extend DataAnnotationsRuleProvider
and override the appropriate method to make it do what you want: extract validation attributes from implemented interfaces. You then register your rule provider with xVal:
ActiveRuleProviders.Providers.Clear();
ActiveRuleProviders.Providers.Add(new MyDataAnnotationsRuleProvider());
ActiveRuleProviders.Providers.Add(new CustomRulesProvider());
To get attributes from properties in implemented interfaces, you should extend DataAnnotationsRuleProvider
and override GetRulesFromTypeCore
. It gets a parameter of type System.Type
that has a method GetInterfaces
.