Traversing DataAnnotationsModelValidatorProvider for a specific type in asp.net mvc

I have a "Content" type (from a custom CMS) that the DataAnnotationsModelValidatorProvider should not check. I do not want to remove DataAnnotationsModelValidatorProvider from providers as I need it for all other types.

I was thinking about creating a custom ModelValidatorProvider that inherits from DataAnnotationsModelValidatorProvider and at the only public entry point (GetValidators method) checks if the containerType is of type "Content", then returns an empty collection of validators and allows another custom ModelValidatorProvider to handle the validation for that particular type for that particular type. But my friends at Microsoft decided to seal this method in the AssociatedValidatorProvider, which is the base class of DataAnnotationsModelValidatorProvider.

Do you see any other way to pass DataAnnotationsModelValidatorProvider for a specific type?

If not, can you vote for my proposal 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 issue: http://forums.asp.net/t/1751517.aspx/1

+3


source to share


1 answer


Could you just inherit directly from ModelValidatorProvider

? Then you can change GetValidators

as you like. 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);
    }
}

      

+7


source







All Articles