Created
January 11, 2013 22:41
-
-
Save martinnormark/4514597 to your computer and use it in GitHub Desktop.
Dynamically parse new validation rules on DOM elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function ($) { | |
$.validator.unobtrusive.parseDynamic = function (selector) { | |
//use the normal unobstrusive.parse method | |
$.validator.unobtrusive.parse(selector); | |
//get the relevant form | |
var form = $(selector).first().closest('form'); | |
if (!form.length) { | |
form = $(selector).first().find('form'); | |
} | |
//get the collections of unobstrusive validators, and jquery validators | |
//and compare the two | |
var unobtrusiveValidation = form.data('unobtrusiveValidation'); | |
var validator = form.validate(); | |
if (!unobtrusiveValidation || ! validator) { | |
return; | |
} | |
$.each(unobtrusiveValidation.options.rules, function (elname, elrules) { | |
if (validator.settings.rules[elname] == undefined) { | |
var args = {}; | |
$.extend(args, elrules); | |
args.messages = unobtrusiveValidation.options.messages[elname]; | |
//edit:use quoted strings for the name selector | |
$("[name='" + elname + "']").rules("add", args); | |
} else { | |
$.each(elrules, function (rulename, data) { | |
if (validator.settings.rules[elname][rulename] == undefined) { | |
var args = {}; | |
args[rulename] = data; | |
args.messages = unobtrusiveValidation.options.messages[elname][rulename]; | |
//edit:use quoted strings for the name selector | |
$("[name='" + elname + "']").rules("add", args); | |
} | |
}); | |
} | |
}); | |
} | |
})($); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
55555555555555555