Cra*_*per 5 data-annotations unobtrusive-validation asp.net-mvc-3 asp.net-mvc-4
我创建了以下自定义RegularExpressionAttribute
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class AlphaNumericAttribute: RegularExpressionAttribute, IClientValidatable
{
public AlphaNumericAttribute()
: base("^[-A-Za-z0-9]+$")
{
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()), ValidationType = "alphanumeric" };
}
}
Run Code Online (Sandbox Code Playgroud)
ViewModel中的字段使用我的AlphaNumeric属性进行修饰:
[AlphaNumeric(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = Resources.DriverLicenseNumber_RegexError_)]
public string DriverLicenseNumber { get; set; }
Run Code Online (Sandbox Code Playgroud)
该字段构建在视图中:
@using (Html.BeginForm("Index", "Application", FormMethod.Post, new { id = "applicationDataForm", autocomplete = "off" }))
{
@Html.LabelFor(m => m.DriverLicenseNumber)
@Html.ValidationMessageFor(m => m.DriverLicenseNumber)
@Html.TextBoxFor(m => m.DriverLicenseNumber)
}
Run Code Online (Sandbox Code Playgroud)
这应该在我的html输入标记上产生适当的"数据 - "验证属性.但是,呈现的标记如下所示:
<input data-val="true" data-val-alphanumeric="Please enter a valid driver's license number." id="DriverLicenseNumber" name="DriverLicenseNumber" type="text" value="" maxlength="20" class="valid">
Run Code Online (Sandbox Code Playgroud)
显然不存在应该呈现的data-val-regex和data-val-regex-pattern属性.
我已经构建了具有完全相同结构的其他验证器,并且它们正常工作,就像这个SSN验证一样,它使用jquery掩码处理屏蔽输入的屏蔽空间:
public class SsnAttribute : RegularExpressionAttribute, IClientValidatable
{
public SsnAttribute()
: base("^([0-9]{3}–[0-9]{2}–[0-9]{4})|([?]{3}–[?]{2}–[?]{4})|([0-9]{9,9})$")
{
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()), ValidationType = "ssn" };
}
Run Code Online (Sandbox Code Playgroud)
}
随着ViewModel上附带的应用程序:
[Ssn(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = Resources.SocialSecurity_RegexError_)]
public new string SocialSecurityNumber { get; set; }
Run Code Online (Sandbox Code Playgroud)
该字段构建在视图中:
@using (Html.BeginForm("Index", "Application", FormMethod.Post, new { id = "applicationDataForm", autocomplete = "off" }))
{
@Html.LabelFor(m => m.SocialSecurityNumber)
@Html.ValidationMessageFor(m => m.SocialSecurityNumber)
@Html.TextBoxFor(m => m.SocialSecurityNumber)
}
Run Code Online (Sandbox Code Playgroud)
此验证属性正确呈现data-val-regex和data-val-regex-pattern属性:
<input class="SSNMask valid" data-val="true" data-val-regex="Please enter a valid social security number." data-val-regex-pattern="^([0-9]{3}–[0-9]{2}–[0-9]{4})|([?]{3}–[?]{2}–[?]{4})|([0-9]{9,9})$" id="SocialSecurityNumber" name="SocialSecurityNumber" type="text" value="" maxlength="22">
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚我在AlphaNumeric属性中缺少什么,它没有呈现相应的html属性.
Ale*_*kiy 10
我相信你的问题AlphaNumericAttribute是你没有为你alphanumeric的验证器类型添加JavaScript适配器.
你的代码中肯定有这样的东西:
$.validator.unobtrusive.adapters.add('ssn', function(options) { /*...*/ });
Run Code Online (Sandbox Code Playgroud)
上面的代码声明了客户端适配器SsnAttribute.请注意,它的名称ssn与ValidationType属性中的set相同ModelClientValidationRule.
要解决问题,AlphaNumericAttribute应该返回,ModelClientValidationRegexRule因为它已经为您的案例(即已经存在的适配器regex)进行了所有必要的设置.
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class AlphaNumericAttribute : RegularExpressionAttribute, IClientValidatable
{
public AlphaNumericAttribute()
: base("^[-A-Za-z0-9]+$")
{
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRegexRule(FormatErrorMessage(metadata.GetDisplayName()), Pattern);
}
}
Run Code Online (Sandbox Code Playgroud)
但是如果在正则表达式验证后面的客户端应该有额外的逻辑,你应该编写并注册你自己的不显眼的适配器.
要获得更大的图像,并且为了更好地了解如何在ASP.NET MVC中实现自定义验证,您可以参阅ASP.NET MVC 3中Brad Wilson Unobtrusive Client Validation的博客文章,请参阅Custom adapters for unusual validators部分.
| 归档时间: |
|
| 查看次数: |
6942 次 |
| 最近记录: |