简体   繁体   中英

MVC3 Unobtrusive Validation not working for custom DataAnnotations attribute

I have a custom attribute that is currently a simple wrapper of the DataAnnotations.RequiredAttribute (I will extend it later, but just trying to get this proof of concept working for now). However, this isn't working with MVC3 unobtrusive validation.

It's a very simple problem, really.

Here is my custom attribute:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class RequiredAttribute : System.ComponentModel.DataAnnotations.RequiredAttribute
{
    public RequiredAttribute()
    {
    }

    public RequiredAttribute(Type errorMessageResourceType, string errorMessageResourceName)
    {
        this.ErrorMessageResourceName = errorMessageResourceName;
        this.ErrorMessageResourceType = errorMessageResourceType;
    }
}

Here are two model properties, one using the custom attribute, one using the DataAnnotations attribute:

[System.ComponentModel.DataAnnotations.Required]
public string FirstName { get; set; }

[CustomValidationAttributes.Required]
public string LastName { get; set; }

Here is the Razor code:

<p>
    @Html.TextBoxFor(model => model.FirstName)
    @Html.ValidationMessageFor(model => model.FirstName)
</p>
<p>
    @Html.TextBoxFor(model => model.LastName)
    @Html.ValidationMessageFor(model => model.LastName)
</p>

And here is the resulting output:

<p>
    <input type="text" value="" name="FirstName id="FirstName" data-val-required="The First Name field is required." data-val="true">
    <span data-valmsg-replace="true" data-valmsg-for="FirstName" class="field-validation-valid"></span>
</p>
<p>
    <input type="text" value="" name="LastName" id="LastName">
    <span data-valmsg-replace="true" data-valmsg-for="LastName" class="field-validation-valid"></span>
</p>

So as you can see, FirstName (using DataAnnotations) is rendered with the necessary html attributes needed for the validators, but LastName (using CustomValidationAttributes) is missing the data-val-required and data-val attributes .

Am I doing something wrong, or is this not supported with MVC3 unobtrusive validation?

Thanks in advance.

As ingo pointed out above in the comments, I ended up having to implement IClientValidatable in order for these to work. So, in my example above, I had to add this to my attribute:

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var modelClientValidationRule = new ModelClientValidationRule
        {
            ErrorMessage = FormatErrorMessage(metadata.DisplayName),
            ValidationType = "required"
        };

        yield return modelClientValidationRule;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM