How can I have a custom ValidationAttribute rendered as a 'data-val-xx' attribute on the client-side?

这一生的挚爱 提交于 2019-12-18 12:32:57

问题


Given a ViewModel that looks like this:

public class Login {
    [Required]
    public string Username { get; set; }

    [Required, CustomValidator]
    public string Password { get; set; }
}

And a View like this (Razor syntax here):

@Html.TextBoxFor(f => f.Password)

I am getting the following markup:

<input type="text"
       value="" 
       data-val-required="This field is required." />

However I would like it to also include a 'data-' attribute for my custom validator.

I want something like this:

<input type="text" 
       value="" 
       data-val-required="This field is required."
       data-val-customvalidator="XYZ" />

How can I achieve this with ASP.NET MVC 3.0?

E.g. Do I need to put some special attribute on my custom validator? Or register it somewhere?


回答1:


Well, MSDN saved me (as it often does).

http://msdn.microsoft.com/en-us/library/ff398048.aspx

So first I have to create an adapter for my validation attribute:

public class CustomAttributeAdapter : DataAnnotationsModelValidator<EmailAttribute>
{
    public CustomAttributeAdapter(
        ModelMetadata metadata,
        ControllerContext context,
        CustomAttribute attribute) :
        base(metadata, context, attribute)
    {
    }

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        ModelClientValidationRule rule = new ModelClientValidationRule()
        {
            ErrorMessage = ErrorMessage,
            ValidationType = "custom"
        };
        return new ModelClientValidationRule[] { rule };
    }
}

(The 'ValidationType' setting must be lower-case for this to work, as this is the post-fix which will be used as an HTML5 attribute - 'data-val-custom'.)

Then all I need to do is register it on Application_Start.

DataAnnotationsModelValidatorProvider.RegisterAdapter(
    typeof(EmailAttribute),
    typeof(EmailAttributeAdapter));

Looking forward to a lot of fun with HTML5 validation. :)



来源:https://stackoverflow.com/questions/4120792/how-can-i-have-a-custom-validationattribute-rendered-as-a-data-val-xx-attribut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!