Reputation: 3406
I have a type "Content" (from a custom CMS) that must not be validated by the DataAnnotationsModelValidatorProvider. I don't want to remove the DataAnnotationsModelValidatorProvider from the providers since I need it for all other types.
I thought of creating a custom ModelValidatorProvider which inherited from DataAnnotationsModelValidatorProvider and in the only public entry point (the method GetValidators) check if the containerType is of type "Content" then return an empty collection of validators and let another custom ModelValidatorProvider handle the validation for this specific type. But my friends at Microsoft decided to sealed this method in the AssociatedValidatorProvider which is the base class of DataAnnotationsModelValidatorProvider.
Do you see any other way of skipping the DataAnnotationsModelValidatorProvider for a specific type?
If not, could you vote for my suggestion over here: http://aspnet.uservoice.com/forums/41201-asp-net-mvc/suggestions/3571625-remove-sealed-on-method-getvalidators-of-type-asso
This is a discussion from someone else facing a similar problem: http://forums.asp.net/t/1751517.aspx/1
Upvotes: 3
Views: 1064
Reputation: 3837
Couldn't you just inherit directly from ModelValidatorProvider
? You could then modify GetValidators
as you wish. For example:
public class CustomValidatorProvider : ModelValidatorProvider
{
private readonly DataAnnotationsModelValidatorProvider _provider = new DataAnnotationsModelValidatorProvider();
public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context)
{
if (metadata.ModelType == typeof(Content) || metadata.ContainerType == typeof(Content))
{
return Enumerable.Empty<ModelValidator>();
}
return _provider.GetValidators(metadata, context);
}
}
Upvotes: 7