如何在MVC3中的TemplateEditor中向模型属性添加验证属性 [英] How to add validation attribute to model property in TemplateEditor in MVC3
问题描述
我有一个 DateTime TemplateEditor,我想为它添加正则表达式验证.我有一个 RegularExpression 属性,我可以用它来装饰模型,但我不想用正则表达式来装饰我所有模型中的每个日期时间属性.
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.
有什么方法可以让我的自定义模板编辑器在为它呈现文本框时添加适当的不显眼的标签?
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();
}
}
接下来,在 Application_Start
的 Global.asax.cs
中注册您的 ModelMetadataValidatorProvider.
Next, register your ModelMetadataValidatorProvider in Global.asax.cs
in Application_Start
.
ModelValidatorProviders.Providers.Clear();
ModelValidatorProviders.Providers.Add(new MyModelMetadataValidatorProvider());
现在,当您访问模型时,一个 RegularExpressionAttribte 将附加到每个 DateTime 字段.您还可以扩展它以提供本地化的 DateTime 正则表达式和消息.
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.
咨询师
这篇关于如何在MVC3中的TemplateEditor中向模型属性添加验证属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!