尝试继承RegularExpressionAttribute,不再验证 [英] trying to inherit RegularExpressionAttribute, no longer validates
问题描述
我正在尝试继承RegularExpressionAttribute
以通过验证SSN来提高可重用性.
I'm trying to inherit the RegularExpressionAttribute
to improve reusability with validating SSNs.
我有以下模型:
public class FooModel
{
[RegularExpression(@"^(?!000)(?!666)(?!9[0-9][0-9])\d{3}[- ]?(?!00)\d{2}[- ]?(?!0000)\d{4}$", ErrorMessage = "The SSN you entered is invalid. If you do not have this number please leave the field blank")]
public string Ssn { get; set; }
}
这将在客户端和服务器上正确验证.我想将冗长的正则表达式封装到自己的验证属性中,如下所示:
which will validate correctly on the client and server. I wanted to encapsulate that lengthy regular expression into its own validation attribute like so:
public class SsnAttribute : RegularExpressionAttribute
{
public SsnAttribute() : base(@"^(?!000)(?!666)(?!9[0-9][0-9])\d{3}[- ]?(?!00)\d{2}[- ]?(?!0000)\d{4}$")
{
ErrorMessage = "SSN is invalid";
}
}
然后我像这样更改了FooModel
:
public class FooModel
{
[Ssn(ErrorMessage = "The SSN you entered is invalid. If you do not have this number please leave the field blank")]
public string Ssn { get; set; }
}
现在,验证不会在客户端上呈现不引人注目的数据属性.我不确定为什么,因为这似乎在本质上应该是同一件事.
Now the validation doesn't render the unobtrusive data attributes on the client. I'm not quite sure as to why, since this seems like the two should essentially be the same thing.
有什么建议吗?
推荐答案
在您的Application_Start
中添加以下行,以将adapater与您的自定义属性相关联,该自定义属性将负责发出客户端验证属性:
In your Application_Start
add the following line to associate an adapater to your custom attribute that will be responsible for emitting the client side validation attributes:
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(SsnAttribute),
typeof(RegularExpressionAttributeAdapter)
);
之所以需要此,是因为RegularExpressionAttribute
的实现方式.它没有实现IClientValidatable
接口,但是具有与之关联的RegularExpressionAttributeAdapter
.
The reason why you need this is the way RegularExpressionAttribute
is implemented. It doesn't implement IClientValidatable
interface but it rather has a RegularExpressionAttributeAdapter
which is associated to it.
在您的情况下,您有一个自RegularExpressionAttribute
派生的自定义属性,但您的属性没有实现IClientValidatable
接口以使客户端验证正常工作,也没有与之关联的属性适配器(与它的属性适配器相反)父类).因此,您的SsnAttribute
应该实现IClientValidatable
接口,或者如我在答案中先前建议的那样关联一个适配器.
In your case you have a custom attribute that derives from RegularExpressionAttribute
but your attribute doesn't implement the IClientValidatable
interface in order for client validation to work nor does it have an attribute adapter associated to it (contrary to its parent class). So your SsnAttribute
should either implement the IClientValidatable
interface or have an adapter associated as suggested previously in my answer.
就我个人而言,实现此自定义验证属性没有多大意义.在这种情况下,一个常数可能就足够了:
This being said personally I don't see much point in implementing this custom validation attribute. A constant might be sufficient in this case:
public const string Ssn = @"^(?!000)(?!666)(?!9[0-9][0-9])\d{3}[- ]?(?!00)\d{2}[- ]?(?!0000)\d{4}$", ErrorMessage = "The SSN you entered is invalid. If you do not have this number please leave the field blank";
然后:
public class FooModel
{
[RegularExpression(Ssn, ErrorMessage = "The SSN you entered is invalid. If you do not have this number please leave the field blank")]
public string Ssn { get; set; }
}
似乎很可读.
这篇关于尝试继承RegularExpressionAttribute,不再验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!