如何添加验证属性

如何添加验证属性

本文介绍了如何添加验证属性,TemplateEditor在MVC3模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DateTime TemplateEditor,我想正则表达式验证添加到它。我有我可以装饰与模型RegularEx pression属性,但我不希望有装点每一个日期时间属性在我所有的车型有正则表达式。

I have a DateTime TemplateEditor and I would like to add regex validation to it. I have a RegularExpression attribute that I could decorate the model with, but I dont want to have to decorate every datetime property in all my models with a regex.

有没有办法,我可以我的自定义TemplateEditor添加相应unobstrusive标签时,它呈现文本框呢?

Is there way I can my custom TemplateEditor add the appropriate unobstrusive tags when it renders the textbox for it?

推荐答案

而不是在模板中添加你的验证,你应该使用自定义插入验证 ModelMetadataValidatorProvider 。首先,创建ModelMetadataProvider类:

Instead of adding your validator in the template, you should insert your validator using a custom ModelMetadataValidatorProvider. First, create your ModelMetadataProvider class:

public class MyModelMetadataValidatorProvider : DataAnnotationsModelValidatorProvider
{

    internal static DataAnnotationsModelValidationFactory DefaultAttributeFactory = Create;
    internal static Dictionary<Type, DataAnnotationsModelValidationFactory> AttributeFactories = new Dictionary<Type, DataAnnotationsModelValidationFactory>() {
        {
            typeof(RegularExpressionAttribute),
            (metadata, context, attribute) => new RegularExpressionAttributeAdapter(metadata, context, (RegularExpressionAttribute)attribute)
        }
    };

    internal static ModelValidator Create(ModelMetadata metadata, ControllerContext context, ValidationAttribute attribute)
    {
        return new DataAnnotationsModelValidator(metadata, context, attribute);
    }

    protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
    {
        List<ModelValidator> vals = base.GetValidators(metadata, context, attributes).ToList();

        // inject our new validator
        if (metadata.ModelType.Name == "DateTime")
        {
            DataAnnotationsModelValidationFactory factory;

            RegularExpressionAttribute regex = new RegularExpressionAttribute(
                "^(((0?[1-9]|1[012])/(0?[1-9]|1\\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\\d)\\d{2}|0?2/29/((19|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$");
            regex.ErrorMessage = "Invalid date format";
            if (!AttributeFactories.TryGetValue(regex.GetType(), out factory))
                factory = DefaultAttributeFactory;

            vals.Add(factory(metadata, context, regex));
        }

        return vals.AsEnumerable();
    }
}

接下来,注册您的ModelMetadataValidatorProvider在的Global.asax.cs 的Application_Start

    ModelValidatorProviders.Providers.Clear();
    ModelValidatorProviders.Providers.Add(new MyModelMetadataValidatorProvider());

现在,当你访问一个模式,RegularEx pressionAttribte将被连接到每个DateTime字段。您还可以扩展这提供本地化的日期时间定期EX pression和消息。

Now, when you access a model, a RegularExpressionAttribte will be attached to each DateTime field. You can also extend this to provide a localized DateTime regular expression and message.

counsellorben

counsellorben

这篇关于如何添加验证属性,TemplateEditor在MVC3模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 04:43