在ASP.NET MVC 3不显眼的审定与RequiredAttribute标签= AllowEmptyString真 [英] RequiredAttribute with AllowEmptyString=true in ASP.NET MVC 3 unobtrusive validation
问题描述
如果我有 [必需的(AllowEmptyStrings = TRUE)]
在我看来模型声明验证始终触发空输入。我发现<一个href=\"http://www.kevinlabranche.com/blog/CommentView,guid,96448d56-6783-4dcc-a0dd-2a16a6496342.aspx\">the文章,这也解释了为什么它会发生。你知道,如果有可用的解决?如果没有,你怎么处理呢?
If i have [Required(AllowEmptyStrings = true)]
declaration in my view model the validation is always triggered on empty inputs. I found the article which explains why it happens. Do you know if there is a fix available? If not, how do you handle it?
推荐答案
的注:我假设你是因为你还使用网络的情况之外的视图模型有AllowEmptyStrings = TRUE;否则它似乎并不像有太大的点到具有必需的属性,允许空字符串在Web场景。的
有三个步骤来处理这个问题:
There are three steps to handle this:
- 创建一个自定义属性适配器,它补充说,确认参数
- 注册您的适配器作为一个适配器工厂
- 重写jQuery验证功能,使空字符串时属性为present
第1步:自定义属性适配器
我修改了RequiredAttributeAdapter在该逻辑地址:
I modified the RequiredAttributeAdapter to add in that logic:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace CustomAttributes
{
/// <summary>Provides an adapter for the <see cref="T:System.Runtime.CompilerServices.RequiredAttributeAttribute" /> attribute.</summary>
public class RequiredAttributeAdapter : DataAnnotationsModelValidator<RequiredAttribute>
{
/// <summary>Initializes a new instance of the <see cref="T:System.Runtime.CompilerServices.RequiredAttributeAttribute" /> class.</summary>
/// <param name="metadata">The model metadata.</param>
/// <param name="context">The controller context.</param>
/// <param name="attribute">The required attribute.</param>
public RequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute)
: base(metadata, context, attribute)
{
}
/// <summary>Gets a list of required-value client validation rules.</summary>
/// <returns>A list of required-value client validation rules.</returns>
public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
var rule = new ModelClientValidationRequiredRule(base.ErrorMessage);
if (base.Attribute.AllowEmptyStrings)
{
//setting "true" rather than bool true which is serialized as "True"
rule.ValidationParameters["allowempty"] = "true";
}
return new ModelClientValidationRequiredRule[] { rule };
}
}
}
第2步:注册这在Global.asax中/的Application_Start()
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
DataAnnotationsModelValidatorProvider.RegisterAdapterFactory(typeof(RequiredAttribute),
(metadata, controllerContext, attribute) => new CustomAttributes.RequiredAttributeAdapter(metadata,
controllerContext, (RequiredAttribute)attribute));
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
第3步:覆盖所要求的jQuery验证功能
这是使用jQuery.validator.addMethod()调用来完成,将我们的定制逻辑,然后调用原有的功能 - 你可以阅读更多关于这种方法的这里。如果你正在使用这个整个网站,也许从你_Layout.cshtml引用的脚本文件。这里是你可以在一个页下降到试验用的样品脚本块:
This is done using the jQuery.validator.addMethod() call, adding our custom logic and then calling the original function - you can read more about this approach here. If you are using this throughout your site, perhaps in a script file referenced from your _Layout.cshtml. Here's a sample script block you can drop in a page to test:
<script>
jQuery.validator.methods.oldRequired = jQuery.validator.methods.required;
jQuery.validator.addMethod("required", function (value, element, param) {
if ($(element).attr('data-val-required-allowempty') == 'true') {
return true;
}
return jQuery.validator.methods.oldRequired.call(this, value, element, param);
},
jQuery.validator.messages.required // use default message
);
</script>
这篇关于在ASP.NET MVC 3不显眼的审定与RequiredAttribute标签= AllowEmptyString真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!