annotate Agendas/trunk/src/Agendas.Web/Scripts/jquery.validate.unobtrusive.js @ 144:a2b14da4902f

Alta y modificación de ponentes (personas)
author Nelo@Guinea.neluz.int
date Mon, 01 Aug 2011 10:25:23 -0300
parents c62b77fc33f4
children
rev   line source
10
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
1 /// <reference path="jquery-1.4.4.js" />
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
2 /// <reference path="jquery.validate.js" />
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
3
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
4 /*!
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
5 ** Unobtrusive validation support library for jQuery and jQuery Validate
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
6 ** Copyright (C) Microsoft Corporation. All rights reserved.
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
7 */
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
8
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
9 /*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: false */
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
10 /*global document: false, jQuery: false */
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
11
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
12 (function ($) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
13 var $jQval = $.validator,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
14 adapters,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
15 data_validation = "unobtrusiveValidation";
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
16
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
17 function setValidationValues(options, ruleName, value) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
18 options.rules[ruleName] = value;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
19 if (options.message) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
20 options.messages[ruleName] = options.message;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
21 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
22 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
23
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
24 function splitAndTrim(value) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
25 return value.replace(/^\s+|\s+$/g, "").split(/\s*,\s*/g);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
26 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
27
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
28 function getModelPrefix(fieldName) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
29 return fieldName.substr(0, fieldName.lastIndexOf(".") + 1);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
30 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
31
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
32 function appendModelPrefix(value, prefix) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
33 if (value.indexOf("*.") === 0) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
34 value = value.replace("*.", prefix);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
35 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
36 return value;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
37 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
38
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
39 function onError(error, inputElement) { // 'this' is the form element
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
40 var container = $(this).find("[data-valmsg-for='" + inputElement[0].name + "']"),
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
41 replace = $.parseJSON(container.attr("data-valmsg-replace")) !== false;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
42
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
43 container.removeClass("field-validation-valid").addClass("field-validation-error");
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
44 error.data("unobtrusiveContainer", container);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
45
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
46 if (replace) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
47 container.empty();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
48 error.removeClass("input-validation-error").appendTo(container);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
49 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
50 else {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
51 error.hide();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
52 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
53 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
54
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
55 function onErrors(form, validator) { // 'this' is the form element
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
56 var container = $(this).find("[data-valmsg-summary=true]"),
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
57 list = container.find("ul");
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
58
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
59 if (list && list.length && validator.errorList.length) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
60 list.empty();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
61 container.addClass("validation-summary-errors").removeClass("validation-summary-valid");
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
62
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
63 $.each(validator.errorList, function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
64 $("<li />").html(this.message).appendTo(list);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
65 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
66 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
67 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
68
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
69 function onSuccess(error) { // 'this' is the form element
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
70 var container = error.data("unobtrusiveContainer"),
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
71 replace = $.parseJSON(container.attr("data-valmsg-replace"));
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
72
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
73 if (container) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
74 container.addClass("field-validation-valid").removeClass("field-validation-error");
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
75 error.removeData("unobtrusiveContainer");
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
76
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
77 if (replace) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
78 container.empty();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
79 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
80 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
81 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
82
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
83 function validationInfo(form) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
84 var $form = $(form),
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
85 result = $form.data(data_validation);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
86
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
87 if (!result) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
88 result = {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
89 options: { // options structure passed to jQuery Validate's validate() method
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
90 errorClass: "input-validation-error",
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
91 errorElement: "span",
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
92 errorPlacement: $.proxy(onError, form),
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
93 invalidHandler: $.proxy(onErrors, form),
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
94 messages: {},
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
95 rules: {},
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
96 success: $.proxy(onSuccess, form)
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
97 },
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
98 attachValidation: function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
99 $form.validate(this.options);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
100 },
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
101 validate: function () { // a validation function that is called by unobtrusive Ajax
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
102 $form.validate();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
103 return $form.valid();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
104 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
105 };
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
106 $form.data(data_validation, result);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
107 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
108
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
109 return result;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
110 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
111
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
112 $jQval.unobtrusive = {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
113 adapters: [],
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
114
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
115 parseElement: function (element, skipAttach) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
116 /// <summary>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
117 /// Parses a single HTML element for unobtrusive validation attributes.
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
118 /// </summary>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
119 /// <param name="element" domElement="true">The HTML element to be parsed.</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
120 /// <param name="skipAttach" type="Boolean">[Optional] true to skip attaching the
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
121 /// validation to the form. If parsing just this single element, you should specify true.
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
122 /// If parsing several elements, you should specify false, and manually attach the validation
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
123 /// to the form when you are finished. The default is false.</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
124 var $element = $(element),
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
125 form = $element.parents("form")[0],
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
126 valInfo, rules, messages;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
127
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
128 if (!form) { // Cannot do client-side validation without a form
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
129 return;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
130 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
131
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
132 valInfo = validationInfo(form);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
133 valInfo.options.rules[element.name] = rules = {};
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
134 valInfo.options.messages[element.name] = messages = {};
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
135
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
136 $.each(this.adapters, function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
137 var prefix = "data-val-" + this.name,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
138 message = $element.attr(prefix),
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
139 paramValues = {};
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
140
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
141 if (message !== undefined) { // Compare against undefined, because an empty message is legal (and falsy)
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
142 prefix += "-";
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
143
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
144 $.each(this.params, function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
145 paramValues[this] = $element.attr(prefix + this);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
146 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
147
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
148 this.adapt({
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
149 element: element,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
150 form: form,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
151 message: message,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
152 params: paramValues,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
153 rules: rules,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
154 messages: messages
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
155 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
156 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
157 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
158
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
159 jQuery.extend(rules, { "__dummy__": true });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
160
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
161 if (!skipAttach) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
162 valInfo.attachValidation();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
163 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
164 },
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
165
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
166 parse: function (selector) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
167 /// <summary>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
168 /// Parses all the HTML elements in the specified selector. It looks for input elements decorated
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
169 /// with the [data-val=true] attribute value and enables validation according to the data-val-*
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
170 /// attribute values.
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
171 /// </summary>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
172 /// <param name="selector" type="String">Any valid jQuery selector.</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
173 $(selector).find(":input[data-val=true]").each(function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
174 $jQval.unobtrusive.parseElement(this, true);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
175 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
176
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
177 $("form").each(function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
178 var info = validationInfo(this);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
179 if (info) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
180 info.attachValidation();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
181 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
182 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
183 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
184 };
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
185
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
186 adapters = $jQval.unobtrusive.adapters;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
187
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
188 adapters.add = function (adapterName, params, fn) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
189 /// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation.</summary>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
190 /// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
191 /// in the data-val-nnnn HTML attribute (where nnnn is the adapter name).</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
192 /// <param name="params" type="Array" optional="true">[Optional] An array of parameter names (strings) that will
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
193 /// be extracted from the data-val-nnnn-mmmm HTML attributes (where nnnn is the adapter name, and
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
194 /// mmmm is the parameter name).</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
195 /// <param name="fn" type="Function">The function to call, which adapts the values from the HTML
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
196 /// attributes into jQuery Validate rules and/or messages.</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
197 /// <returns type="jQuery.validator.unobtrusive.adapters" />
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
198 if (!fn) { // Called with no params, just a function
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
199 fn = params;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
200 params = [];
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
201 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
202 this.push({ name: adapterName, params: params, adapt: fn });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
203 return this;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
204 };
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
205
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
206 adapters.addBool = function (adapterName, ruleName) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
207 /// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation, where
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
208 /// the jQuery Validate validation rule has no parameter values.</summary>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
209 /// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
210 /// in the data-val-nnnn HTML attribute (where nnnn is the adapter name).</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
211 /// <param name="ruleName" type="String" optional="true">[Optional] The name of the jQuery Validate rule. If not provided, the value
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
212 /// of adapterName will be used instead.</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
213 /// <returns type="jQuery.validator.unobtrusive.adapters" />
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
214 return this.add(adapterName, function (options) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
215 setValidationValues(options, ruleName || adapterName, true);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
216 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
217 };
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
218
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
219 adapters.addMinMax = function (adapterName, minRuleName, maxRuleName, minMaxRuleName, minAttribute, maxAttribute) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
220 /// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation, where
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
221 /// the jQuery Validate validation has three potential rules (one for min-only, one for max-only, and
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
222 /// one for min-and-max). The HTML parameters are expected to be named -min and -max.</summary>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
223 /// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
224 /// in the data-val-nnnn HTML attribute (where nnnn is the adapter name).</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
225 /// <param name="minRuleName" type="String">The name of the jQuery Validate rule to be used when you only
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
226 /// have a minimum value.</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
227 /// <param name="maxRuleName" type="String">The name of the jQuery Validate rule to be used when you only
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
228 /// have a maximum value.</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
229 /// <param name="minMaxRuleName" type="String">The name of the jQuery Validate rule to be used when you
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
230 /// have both a minimum and maximum value.</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
231 /// <param name="minAttribute" type="String" optional="true">[Optional] The name of the HTML attribute that
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
232 /// contains the minimum value. The default is "min".</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
233 /// <param name="maxAttribute" type="String" optional="true">[Optional] The name of the HTML attribute that
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
234 /// contains the maximum value. The default is "max".</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
235 /// <returns type="jQuery.validator.unobtrusive.adapters" />
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
236 return this.add(adapterName, [minAttribute || "min", maxAttribute || "max"], function (options) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
237 var min = options.params.min,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
238 max = options.params.max;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
239
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
240 if (min && max) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
241 setValidationValues(options, minMaxRuleName, [min, max]);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
242 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
243 else if (min) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
244 setValidationValues(options, minRuleName, min);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
245 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
246 else if (max) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
247 setValidationValues(options, maxRuleName, max);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
248 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
249 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
250 };
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
251
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
252 adapters.addSingleVal = function (adapterName, attribute, ruleName) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
253 /// <summary>Adds a new adapter to convert unobtrusive HTML into a jQuery Validate validation, where
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
254 /// the jQuery Validate validation rule has a single value.</summary>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
255 /// <param name="adapterName" type="String">The name of the adapter to be added. This matches the name used
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
256 /// in the data-val-nnnn HTML attribute(where nnnn is the adapter name).</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
257 /// <param name="attribute" type="String">[Optional] The name of the HTML attribute that contains the value.
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
258 /// The default is "val".</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
259 /// <param name="ruleName" type="String" optional="true">[Optional] The name of the jQuery Validate rule. If not provided, the value
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
260 /// of adapterName will be used instead.</param>
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
261 /// <returns type="jQuery.validator.unobtrusive.adapters" />
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
262 return this.add(adapterName, [attribute || "val"], function (options) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
263 setValidationValues(options, ruleName || adapterName, options.params[attribute]);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
264 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
265 };
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
266
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
267 $jQval.addMethod("__dummy__", function (value, element, params) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
268 return true;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
269 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
270
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
271 $jQval.addMethod("regex", function (value, element, params) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
272 var match;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
273 if (this.optional(element)) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
274 return true;
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
275 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
276
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
277 match = new RegExp(params).exec(value);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
278 return (match && (match.index === 0) && (match[0].length === value.length));
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
279 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
280
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
281 adapters.addSingleVal("accept", "exts").addSingleVal("regex", "pattern");
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
282 adapters.addBool("creditcard").addBool("date").addBool("digits").addBool("email").addBool("number").addBool("url");
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
283 adapters.addMinMax("length", "minlength", "maxlength", "rangelength").addMinMax("range", "min", "max", "range");
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
284 adapters.add("equalto", ["other"], function (options) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
285 var prefix = getModelPrefix(options.element.name),
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
286 other = options.params.other,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
287 fullOtherName = appendModelPrefix(other, prefix),
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
288 element = $(options.form).find(":input[name=" + fullOtherName + "]")[0];
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
289
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
290 setValidationValues(options, "equalTo", element);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
291 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
292 adapters.add("required", function (options) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
293 // jQuery Validate equates "required" with "mandatory" for checkbox elements
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
294 if (options.element.tagName.toUpperCase() !== "INPUT" || options.element.type.toUpperCase() !== "CHECKBOX") {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
295 setValidationValues(options, "required", true);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
296 }
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
297 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
298 adapters.add("remote", ["url", "type", "additionalfields"], function (options) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
299 var value = {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
300 url: options.params.url,
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
301 type: options.params.type || "GET",
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
302 data: {}
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
303 },
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
304 prefix = getModelPrefix(options.element.name);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
305
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
306 $.each(splitAndTrim(options.params.additionalfields || options.element.name), function (i, fieldName) {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
307 var paramName = appendModelPrefix(fieldName, prefix);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
308 value.data[paramName] = function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
309 return $(options.form).find(":input[name='" + paramName + "']").val();
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
310 };
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
311 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
312
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
313 setValidationValues(options, "remote", value);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
314 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
315
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
316 $(function () {
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
317 $jQval.unobtrusive.parse(document);
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
318 });
c62b77fc33f4 website inicial
nelo@MTEySS.neluz.int
parents:
diff changeset
319 }(jQuery));