Updated Unobtrusive Validation to 2.0.20126

pull/4/head
kay.one 13 years ago
parent 9d126e54c9
commit 0b59bd1312

@ -1,7 +1,4 @@
/// <reference path="jquery-1.5.1.js" /> /*!
/// <reference path="jquery.validate.js" />
/*!
** Unobtrusive validation support library for jQuery and jQuery Validate ** Unobtrusive validation support library for jQuery and jQuery Validate
** Copyright (C) Microsoft Corporation. All rights reserved. ** Copyright (C) Microsoft Corporation. All rights reserved.
*/ */
@ -57,7 +54,7 @@
} }
} }
function onErrors(form, validator) { // 'this' is the form element function onErrors(event, validator) { // 'this' is the form element
var container = $(this).find("[data-valmsg-summary=true]"), var container = $(this).find("[data-valmsg-summary=true]"),
list = container.find("ul"); list = container.find("ul");
@ -85,9 +82,24 @@
} }
} }
function onReset(event) { // 'this' is the form element
var $form = $(this);
$form.data("validator").resetForm();
$form.find(".validation-summary-errors")
.addClass("validation-summary-valid")
.removeClass("validation-summary-errors");
$form.find(".field-validation-error")
.addClass("field-validation-valid")
.removeClass("field-validation-error")
.removeData("unobtrusiveContainer")
.find(">*") // If we were using valmsg-replace, get the underlying error
.removeData("unobtrusiveContainer");
}
function validationInfo(form) { function validationInfo(form) {
var $form = $(form), var $form = $(form),
result = $form.data(data_validation); result = $form.data(data_validation),
onResetProxy = $.proxy(onReset, form);
if (!result) { if (!result) {
result = { result = {
@ -101,7 +113,10 @@
success: $.proxy(onSuccess, form) success: $.proxy(onSuccess, form)
}, },
attachValidation: function () { attachValidation: function () {
$form.validate(this.options); $form
.unbind("reset." + data_validation, onResetProxy)
.bind("reset." + data_validation, onResetProxy)
.validate(this.options);
}, },
validate: function () { // a validation function that is called by unobtrusive Ajax validate: function () { // a validation function that is called by unobtrusive Ajax
$form.validate(); $form.validate();
@ -161,7 +176,7 @@
} }
}); });
jQuery.extend(rules, { "__dummy__": true }); $.extend(rules, { "__dummy__": true });
if (!skipAttach) { if (!skipAttach) {
valInfo.attachValidation(); valInfo.attachValidation();
@ -175,11 +190,17 @@
/// attribute values. /// attribute values.
/// </summary> /// </summary>
/// <param name="selector" type="String">Any valid jQuery selector.</param> /// <param name="selector" type="String">Any valid jQuery selector.</param>
var $forms = $(selector)
.parents("form")
.andSelf()
.add($(selector).find("form"))
.filter("form");
$(selector).find(":input[data-val=true]").each(function () { $(selector).find(":input[data-val=true]").each(function () {
$jQval.unobtrusive.parseElement(this, true); $jQval.unobtrusive.parseElement(this, true);
}); });
$("form").each(function () { $forms.each(function () {
var info = validationInfo(this); var info = validationInfo(this);
if (info) { if (info) {
info.attachValidation(); info.attachValidation();
@ -290,7 +311,7 @@
var prefix = getModelPrefix(options.element.name), var prefix = getModelPrefix(options.element.name),
other = options.params.other, other = options.params.other,
fullOtherName = appendModelPrefix(other, prefix), fullOtherName = appendModelPrefix(other, prefix),
element = $(options.form).find(":input[name=" + escapeAttributeValue(fullOtherName) + "]")[0]; element = $(options.form).find(":input[name='" + escapeAttributeValue(fullOtherName) + "']")[0];
setValidationValues(options, "equalTo", element); setValidationValues(options, "equalTo", element);
}); });

@ -9,7 +9,7 @@
<package id="jQuery.UI.Combined" version="1.8.17" /> <package id="jQuery.UI.Combined" version="1.8.17" />
<package id="jQuery.Validation" version="1.8.0.1" /> <package id="jQuery.Validation" version="1.8.0.1" />
<package id="jQuery.Validation" version="1.9" /> <package id="jQuery.Validation" version="1.9" />
<package id="jQuery.Validation.Unobtrusive" version="1.0" /> <package id="jQuery.Validation.Unobtrusive" version="2.0.20126.16343" />
<package id="jQuery.vsdoc" version="1.6" /> <package id="jQuery.vsdoc" version="1.6" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" /> <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" />
<package id="MiniProfiler" version="1.9" /> <package id="MiniProfiler" version="1.9" />

@ -1,7 +1,4 @@
/// <reference path="jquery-1.5.1.js" /> /*!
/// <reference path="jquery.validate.js" />
/*!
** Unobtrusive validation support library for jQuery and jQuery Validate ** Unobtrusive validation support library for jQuery and jQuery Validate
** Copyright (C) Microsoft Corporation. All rights reserved. ** Copyright (C) Microsoft Corporation. All rights reserved.
*/ */
@ -57,7 +54,7 @@
} }
} }
function onErrors(form, validator) { // 'this' is the form element function onErrors(event, validator) { // 'this' is the form element
var container = $(this).find("[data-valmsg-summary=true]"), var container = $(this).find("[data-valmsg-summary=true]"),
list = container.find("ul"); list = container.find("ul");
@ -85,9 +82,24 @@
} }
} }
function onReset(event) { // 'this' is the form element
var $form = $(this);
$form.data("validator").resetForm();
$form.find(".validation-summary-errors")
.addClass("validation-summary-valid")
.removeClass("validation-summary-errors");
$form.find(".field-validation-error")
.addClass("field-validation-valid")
.removeClass("field-validation-error")
.removeData("unobtrusiveContainer")
.find(">*") // If we were using valmsg-replace, get the underlying error
.removeData("unobtrusiveContainer");
}
function validationInfo(form) { function validationInfo(form) {
var $form = $(form), var $form = $(form),
result = $form.data(data_validation); result = $form.data(data_validation),
onResetProxy = $.proxy(onReset, form);
if (!result) { if (!result) {
result = { result = {
@ -101,7 +113,10 @@
success: $.proxy(onSuccess, form) success: $.proxy(onSuccess, form)
}, },
attachValidation: function () { attachValidation: function () {
$form.validate(this.options); $form
.unbind("reset." + data_validation, onResetProxy)
.bind("reset." + data_validation, onResetProxy)
.validate(this.options);
}, },
validate: function () { // a validation function that is called by unobtrusive Ajax validate: function () { // a validation function that is called by unobtrusive Ajax
$form.validate(); $form.validate();
@ -161,7 +176,7 @@
} }
}); });
jQuery.extend(rules, { "__dummy__": true }); $.extend(rules, { "__dummy__": true });
if (!skipAttach) { if (!skipAttach) {
valInfo.attachValidation(); valInfo.attachValidation();
@ -175,11 +190,17 @@
/// attribute values. /// attribute values.
/// </summary> /// </summary>
/// <param name="selector" type="String">Any valid jQuery selector.</param> /// <param name="selector" type="String">Any valid jQuery selector.</param>
var $forms = $(selector)
.parents("form")
.andSelf()
.add($(selector).find("form"))
.filter("form");
$(selector).find(":input[data-val=true]").each(function () { $(selector).find(":input[data-val=true]").each(function () {
$jQval.unobtrusive.parseElement(this, true); $jQval.unobtrusive.parseElement(this, true);
}); });
$("form").each(function () { $forms.each(function () {
var info = validationInfo(this); var info = validationInfo(this);
if (info) { if (info) {
info.attachValidation(); info.attachValidation();
@ -290,7 +311,7 @@
var prefix = getModelPrefix(options.element.name), var prefix = getModelPrefix(options.element.name),
other = options.params.other, other = options.params.other,
fullOtherName = appendModelPrefix(other, prefix), fullOtherName = appendModelPrefix(other, prefix),
element = $(options.form).find(":input[name=" + escapeAttributeValue(fullOtherName) + "]")[0]; element = $(options.form).find(":input[name='" + escapeAttributeValue(fullOtherName) + "']")[0];
setValidationValues(options, "equalTo", element); setValidationValues(options, "equalTo", element);
}); });

@ -0,0 +1,5 @@
/*
** Unobtrusive validation support library for jQuery and jQuery Validate
** Copyright (C) Microsoft Corporation. All rights reserved.
*/
(function(a){var d=a.validator,b,e="unobtrusiveValidation";function c(a,b,c){a.rules[b]=c;if(a.message)a.messages[b]=a.message}function j(a){return a.replace(/^\s+|\s+$/g,"").split(/\s*,\s*/g)}function f(a){return a.replace(/([!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~])/g,"\\$1")}function h(a){return a.substr(0,a.lastIndexOf(".")+1)}function g(a,b){if(a.indexOf("*.")===0)a=a.replace("*.",b);return a}function m(c,d){var b=a(this).find("[data-valmsg-for='"+f(d[0].name)+"']"),e=a.parseJSON(b.attr("data-valmsg-replace"))!==false;b.removeClass("field-validation-valid").addClass("field-validation-error");c.data("unobtrusiveContainer",b);if(e){b.empty();c.removeClass("input-validation-error").appendTo(b)}else c.hide()}function l(e,d){var c=a(this).find("[data-valmsg-summary=true]"),b=c.find("ul");if(b&&b.length&&d.errorList.length){b.empty();c.addClass("validation-summary-errors").removeClass("validation-summary-valid");a.each(d.errorList,function(){a("<li />").html(this.message).appendTo(b)})}}function k(c){var b=c.data("unobtrusiveContainer"),d=a.parseJSON(b.attr("data-valmsg-replace"));if(b){b.addClass("field-validation-valid").removeClass("field-validation-error");c.removeData("unobtrusiveContainer");d&&b.empty()}}function n(){var b=a(this);b.data("validator").resetForm();b.find(".validation-summary-errors").addClass("validation-summary-valid").removeClass("validation-summary-errors");b.find(".field-validation-error").addClass("field-validation-valid").removeClass("field-validation-error").removeData("unobtrusiveContainer").find(">*").removeData("unobtrusiveContainer")}function i(c){var b=a(c),d=b.data(e),f=a.proxy(n,c);if(!d){d={options:{errorClass:"input-validation-error",errorElement:"span",errorPlacement:a.proxy(m,c),invalidHandler:a.proxy(l,c),messages:{},rules:{},success:a.proxy(k,c)},attachValidation:function(){b.unbind("reset."+e,f).bind("reset."+e,f).validate(this.options)},validate:function(){b.validate();return b.valid()}};b.data(e,d)}return d}d.unobtrusive={adapters:[],parseElement:function(b,h){var d=a(b),f=d.parents("form")[0],c,e,g;if(!f)return;c=i(f);c.options.rules[b.name]=e={};c.options.messages[b.name]=g={};a.each(this.adapters,function(){var c="data-val-"+this.name,i=d.attr(c),h={};if(i!==undefined){c+="-";a.each(this.params,function(){h[this]=d.attr(c+this)});this.adapt({element:b,form:f,message:i,params:h,rules:e,messages:g})}});a.extend(e,{__dummy__:true});!h&&c.attachValidation()},parse:function(b){var c=a(b).parents("form").andSelf().add(a(b).find("form")).filter("form");a(b).find(":input[data-val=true]").each(function(){d.unobtrusive.parseElement(this,true)});c.each(function(){var a=i(this);a&&a.attachValidation()})}};b=d.unobtrusive.adapters;b.add=function(c,a,b){if(!b){b=a;a=[]}this.push({name:c,params:a,adapt:b});return this};b.addBool=function(a,b){return this.add(a,function(d){c(d,b||a,true)})};b.addMinMax=function(e,g,f,a,d,b){return this.add(e,[d||"min",b||"max"],function(b){var e=b.params.min,d=b.params.max;if(e&&d)c(b,a,[e,d]);else if(e)c(b,g,e);else d&&c(b,f,d)})};b.addSingleVal=function(a,b,d){return this.add(a,[b||"val"],function(e){c(e,d||a,e.params[b])})};d.addMethod("__dummy__",function(){return true});d.addMethod("regex",function(b,c,d){var a;if(this.optional(c))return true;a=(new RegExp(d)).exec(b);return a&&a.index===0&&a[0].length===b.length});b.addSingleVal("accept","exts").addSingleVal("regex","pattern");b.addBool("creditcard").addBool("date").addBool("digits").addBool("email").addBool("number").addBool("url");b.addMinMax("length","minlength","maxlength","rangelength").addMinMax("range","min","max","range");b.add("equalto",["other"],function(b){var i=h(b.element.name),j=b.params.other,d=g(j,i),e=a(b.form).find(":input[name='"+f(d)+"']")[0];c(b,"equalTo",e)});b.add("required",function(a){(a.element.tagName.toUpperCase()!=="INPUT"||a.element.type.toUpperCase()!=="CHECKBOX")&&c(a,"required",true)});b.add("remote",["url","type","additionalfields"],function(b){var d={url:b.params.url,type:b.params.type||"GET",data:{}},e=h(b.element.name);a.each(j(b.params.additionalfields||b.element.name),function(i,h){var c=g(h,e);d.data[c]=function(){return a(b.form).find(":input[name='"+f(c)+"']").val()}});c(b,"remote",d)});a(function(){d.unobtrusive.parse(document)})})(jQuery);
Loading…
Cancel
Save