ASP.Net MVC Unobtrusive Validation Bug

If you use the ASP.Net MVC 3 [Compare] validation attribute on a model property, then include that model as a property in a parent model (so that the field name becomes Parent.ChildProperty), the built-in unobtrusive client validation will choke, and will always report the field as having an error.

This is due to a bug on line 288 of jquery.validate.unobtrusive.js:

adapters.add("equalto", ["other"], function (options) {
    var prefix = getModelPrefix(options.element.name),
        other = options.params.other,
        fullOtherName = appendModelPrefix(other, prefix),
        element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];

    setValidationValues(options, "equalTo", element);
});

Because the value of the name attribute selector is not quoted, this fails if the name contains a ..

The simplest fix is to add quotes around the concatenated value.  However, the jQuery selector there is overkill.  HTML form elements have properties for each named input, so you can do this instead:

adapters.add("equalto", ["other"], function (options) {
    var prefix = getModelPrefix(options.element.name),
        other = options.params.other,
        fullOtherName = appendModelPrefix(other, prefix),
        element = options.form[fullOtherName];
    if (!element)
        throw new Error(fullOtherName + " not found");
    //If there are multiple inputs with that name, get the first one
    if (element.length && element[0])        
        element = element[0];
    setValidationValues(options, "equalTo", element);
});

19 comments:

Thank you very much. Very complete guide. gmail login

This is useful info about ASP.Net MVC 3. You are very kind when sharing this hotmail log in

Education is really important today. That's why I know that it will be useful for you to know how to write a cover letter. It was helpful for me when I studied in college.

This is useful info about ASP.Net MVC 3.
Android Stock Firmware

You are very kind when sharing this
Mi Home Apk

Thank you very much. Very complete guide.
Download Android USB Drivers

That's why I know that it will be useful for you to know
Driving Zone 2 Mod Apk

It was helpful for me when I studied in college.
Creative Destruction PC

Thank you very much. Very complete guide
Run, sausage, run For PC

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

Post a Comment