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);
});

26 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 :)

Thank you for sharing! I hope you will continue to have great articles like this to share with everyone!
Mobile app development company in toronto

Nice and interesting post,I appreciate your hard work,keep uploading more, Thank you for sharing valuable information.
Mobile app Development Company in Delhi

Useful information for all. It's exactly what I tell my corporate clients(ESL/EFL students)when doing a writing workshop. The main difficulty for us teachers, and/or educators, is when people are so convinced that their Franglais, or Germanglais, or whatever, is correct. On the other hand, the non-native speakers go nuts when bosses, or collaborators, don't write proper emails themselves.

https://is.gd/WTU8yj

Dell Printer Support is a team of experts who takes care of your dell printer issue. Our support is capable of dealing with any dell printer glitches. In case you're having issue with you dell printer contact dell Printer Support and get instant help.

AOL Tech Support covers all your AOL mail related issues. The experts at AOL Tech Support are quite proficient in dealing with your issues with ease. For any help related to AOL, contact AOL Tech Support

Post a Comment