10
|
1 //!----------------------------------------------------------
|
|
2 //! Copyright (C) Microsoft Corporation. All rights reserved.
|
|
3 //!----------------------------------------------------------
|
|
4 //! MicrosoftMvcValidation.js
|
|
5
|
|
6
|
|
7 Type.registerNamespace('Sys.Mvc');
|
|
8
|
|
9 ////////////////////////////////////////////////////////////////////////////////
|
|
10 // Sys.Mvc.Validation
|
|
11
|
|
12 Sys.Mvc.$create_Validation = function Sys_Mvc_Validation() { return {}; }
|
|
13
|
|
14
|
|
15 ////////////////////////////////////////////////////////////////////////////////
|
|
16 // Sys.Mvc.JsonValidationField
|
|
17
|
|
18 Sys.Mvc.$create_JsonValidationField = function Sys_Mvc_JsonValidationField() { return {}; }
|
|
19
|
|
20
|
|
21 ////////////////////////////////////////////////////////////////////////////////
|
|
22 // Sys.Mvc.JsonValidationOptions
|
|
23
|
|
24 Sys.Mvc.$create_JsonValidationOptions = function Sys_Mvc_JsonValidationOptions() { return {}; }
|
|
25
|
|
26
|
|
27 ////////////////////////////////////////////////////////////////////////////////
|
|
28 // Sys.Mvc.JsonValidationRule
|
|
29
|
|
30 Sys.Mvc.$create_JsonValidationRule = function Sys_Mvc_JsonValidationRule() { return {}; }
|
|
31
|
|
32
|
|
33 ////////////////////////////////////////////////////////////////////////////////
|
|
34 // Sys.Mvc.ValidationContext
|
|
35
|
|
36 Sys.Mvc.$create_ValidationContext = function Sys_Mvc_ValidationContext() { return {}; }
|
|
37
|
|
38
|
|
39 ////////////////////////////////////////////////////////////////////////////////
|
|
40 // Sys.Mvc.NumberValidator
|
|
41
|
|
42 Sys.Mvc.NumberValidator = function Sys_Mvc_NumberValidator() {
|
|
43 }
|
|
44 Sys.Mvc.NumberValidator.create = function Sys_Mvc_NumberValidator$create(rule) {
|
|
45 /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
|
|
46 /// </param>
|
|
47 /// <returns type="Sys.Mvc.Validator"></returns>
|
|
48 return Function.createDelegate(new Sys.Mvc.NumberValidator(), new Sys.Mvc.NumberValidator().validate);
|
|
49 }
|
|
50 Sys.Mvc.NumberValidator.prototype = {
|
|
51
|
|
52 validate: function Sys_Mvc_NumberValidator$validate(value, context) {
|
|
53 /// <param name="value" type="String">
|
|
54 /// </param>
|
|
55 /// <param name="context" type="Sys.Mvc.ValidationContext">
|
|
56 /// </param>
|
|
57 /// <returns type="Object"></returns>
|
|
58 if (Sys.Mvc._validationUtil.stringIsNullOrEmpty(value)) {
|
|
59 return true;
|
|
60 }
|
|
61 var n = Number.parseLocale(value);
|
|
62 return (!isNaN(n));
|
|
63 }
|
|
64 }
|
|
65
|
|
66
|
|
67 ////////////////////////////////////////////////////////////////////////////////
|
|
68 // Sys.Mvc.FormContext
|
|
69
|
|
70 Sys.Mvc.FormContext = function Sys_Mvc_FormContext(formElement, validationSummaryElement) {
|
|
71 /// <param name="formElement" type="Object" domElement="true">
|
|
72 /// </param>
|
|
73 /// <param name="validationSummaryElement" type="Object" domElement="true">
|
|
74 /// </param>
|
|
75 /// <field name="_validationSummaryErrorCss" type="String" static="true">
|
|
76 /// </field>
|
|
77 /// <field name="_validationSummaryValidCss" type="String" static="true">
|
|
78 /// </field>
|
|
79 /// <field name="_formValidationTag" type="String" static="true">
|
|
80 /// </field>
|
|
81 /// <field name="_onClickHandler" type="Sys.UI.DomEventHandler">
|
|
82 /// </field>
|
|
83 /// <field name="_onSubmitHandler" type="Sys.UI.DomEventHandler">
|
|
84 /// </field>
|
|
85 /// <field name="_errors" type="Array">
|
|
86 /// </field>
|
|
87 /// <field name="_submitButtonClicked" type="Object" domElement="true">
|
|
88 /// </field>
|
|
89 /// <field name="_validationSummaryElement" type="Object" domElement="true">
|
|
90 /// </field>
|
|
91 /// <field name="_validationSummaryULElement" type="Object" domElement="true">
|
|
92 /// </field>
|
|
93 /// <field name="fields" type="Array" elementType="FieldContext">
|
|
94 /// </field>
|
|
95 /// <field name="_formElement" type="Object" domElement="true">
|
|
96 /// </field>
|
|
97 /// <field name="replaceValidationSummary" type="Boolean">
|
|
98 /// </field>
|
|
99 this._errors = [];
|
|
100 this.fields = new Array(0);
|
|
101 this._formElement = formElement;
|
|
102 this._validationSummaryElement = validationSummaryElement;
|
|
103 formElement[Sys.Mvc.FormContext._formValidationTag] = this;
|
|
104 if (validationSummaryElement) {
|
|
105 var ulElements = validationSummaryElement.getElementsByTagName('ul');
|
|
106 if (ulElements.length > 0) {
|
|
107 this._validationSummaryULElement = ulElements[0];
|
|
108 }
|
|
109 }
|
|
110 this._onClickHandler = Function.createDelegate(this, this._form_OnClick);
|
|
111 this._onSubmitHandler = Function.createDelegate(this, this._form_OnSubmit);
|
|
112 }
|
|
113 Sys.Mvc.FormContext._Application_Load = function Sys_Mvc_FormContext$_Application_Load() {
|
|
114 var allFormOptions = window.mvcClientValidationMetadata;
|
|
115 if (allFormOptions) {
|
|
116 while (allFormOptions.length > 0) {
|
|
117 var thisFormOptions = allFormOptions.pop();
|
|
118 Sys.Mvc.FormContext._parseJsonOptions(thisFormOptions);
|
|
119 }
|
|
120 }
|
|
121 }
|
|
122 Sys.Mvc.FormContext._getFormElementsWithName = function Sys_Mvc_FormContext$_getFormElementsWithName(formElement, name) {
|
|
123 /// <param name="formElement" type="Object" domElement="true">
|
|
124 /// </param>
|
|
125 /// <param name="name" type="String">
|
|
126 /// </param>
|
|
127 /// <returns type="Array" elementType="Object" elementDomElement="true"></returns>
|
|
128 var allElementsWithNameInForm = [];
|
|
129 var allElementsWithName = document.getElementsByName(name);
|
|
130 for (var i = 0; i < allElementsWithName.length; i++) {
|
|
131 var thisElement = allElementsWithName[i];
|
|
132 if (Sys.Mvc.FormContext._isElementInHierarchy(formElement, thisElement)) {
|
|
133 Array.add(allElementsWithNameInForm, thisElement);
|
|
134 }
|
|
135 }
|
|
136 return allElementsWithNameInForm;
|
|
137 }
|
|
138 Sys.Mvc.FormContext.getValidationForForm = function Sys_Mvc_FormContext$getValidationForForm(formElement) {
|
|
139 /// <param name="formElement" type="Object" domElement="true">
|
|
140 /// </param>
|
|
141 /// <returns type="Sys.Mvc.FormContext"></returns>
|
|
142 return formElement[Sys.Mvc.FormContext._formValidationTag];
|
|
143 }
|
|
144 Sys.Mvc.FormContext._isElementInHierarchy = function Sys_Mvc_FormContext$_isElementInHierarchy(parent, child) {
|
|
145 /// <param name="parent" type="Object" domElement="true">
|
|
146 /// </param>
|
|
147 /// <param name="child" type="Object" domElement="true">
|
|
148 /// </param>
|
|
149 /// <returns type="Boolean"></returns>
|
|
150 while (child) {
|
|
151 if (parent === child) {
|
|
152 return true;
|
|
153 }
|
|
154 child = child.parentNode;
|
|
155 }
|
|
156 return false;
|
|
157 }
|
|
158 Sys.Mvc.FormContext._parseJsonOptions = function Sys_Mvc_FormContext$_parseJsonOptions(options) {
|
|
159 /// <param name="options" type="Sys.Mvc.JsonValidationOptions">
|
|
160 /// </param>
|
|
161 /// <returns type="Sys.Mvc.FormContext"></returns>
|
|
162 var formElement = $get(options.FormId);
|
|
163 var validationSummaryElement = (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(options.ValidationSummaryId)) ? $get(options.ValidationSummaryId) : null;
|
|
164 var formContext = new Sys.Mvc.FormContext(formElement, validationSummaryElement);
|
|
165 formContext.enableDynamicValidation();
|
|
166 formContext.replaceValidationSummary = options.ReplaceValidationSummary;
|
|
167 for (var i = 0; i < options.Fields.length; i++) {
|
|
168 var field = options.Fields[i];
|
|
169 var fieldElements = Sys.Mvc.FormContext._getFormElementsWithName(formElement, field.FieldName);
|
|
170 var validationMessageElement = (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(field.ValidationMessageId)) ? $get(field.ValidationMessageId) : null;
|
|
171 var fieldContext = new Sys.Mvc.FieldContext(formContext);
|
|
172 Array.addRange(fieldContext.elements, fieldElements);
|
|
173 fieldContext.validationMessageElement = validationMessageElement;
|
|
174 fieldContext.replaceValidationMessageContents = field.ReplaceValidationMessageContents;
|
|
175 for (var j = 0; j < field.ValidationRules.length; j++) {
|
|
176 var rule = field.ValidationRules[j];
|
|
177 var validator = Sys.Mvc.ValidatorRegistry.getValidator(rule);
|
|
178 if (validator) {
|
|
179 var validation = Sys.Mvc.$create_Validation();
|
|
180 validation.fieldErrorMessage = rule.ErrorMessage;
|
|
181 validation.validator = validator;
|
|
182 Array.add(fieldContext.validations, validation);
|
|
183 }
|
|
184 }
|
|
185 fieldContext.enableDynamicValidation();
|
|
186 Array.add(formContext.fields, fieldContext);
|
|
187 }
|
|
188 var registeredValidatorCallbacks = formElement.validationCallbacks;
|
|
189 if (!registeredValidatorCallbacks) {
|
|
190 registeredValidatorCallbacks = [];
|
|
191 formElement.validationCallbacks = registeredValidatorCallbacks;
|
|
192 }
|
|
193 registeredValidatorCallbacks.push(Function.createDelegate(null, function() {
|
|
194 return Sys.Mvc._validationUtil.arrayIsNullOrEmpty(formContext.validate('submit'));
|
|
195 }));
|
|
196 return formContext;
|
|
197 }
|
|
198 Sys.Mvc.FormContext.prototype = {
|
|
199 _onClickHandler: null,
|
|
200 _onSubmitHandler: null,
|
|
201 _submitButtonClicked: null,
|
|
202 _validationSummaryElement: null,
|
|
203 _validationSummaryULElement: null,
|
|
204 _formElement: null,
|
|
205 replaceValidationSummary: false,
|
|
206
|
|
207 addError: function Sys_Mvc_FormContext$addError(message) {
|
|
208 /// <param name="message" type="String">
|
|
209 /// </param>
|
|
210 this.addErrors([ message ]);
|
|
211 },
|
|
212
|
|
213 addErrors: function Sys_Mvc_FormContext$addErrors(messages) {
|
|
214 /// <param name="messages" type="Array" elementType="String">
|
|
215 /// </param>
|
|
216 if (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(messages)) {
|
|
217 Array.addRange(this._errors, messages);
|
|
218 this._onErrorCountChanged();
|
|
219 }
|
|
220 },
|
|
221
|
|
222 clearErrors: function Sys_Mvc_FormContext$clearErrors() {
|
|
223 Array.clear(this._errors);
|
|
224 this._onErrorCountChanged();
|
|
225 },
|
|
226
|
|
227 _displayError: function Sys_Mvc_FormContext$_displayError() {
|
|
228 if (this._validationSummaryElement) {
|
|
229 if (this._validationSummaryULElement) {
|
|
230 Sys.Mvc._validationUtil.removeAllChildren(this._validationSummaryULElement);
|
|
231 for (var i = 0; i < this._errors.length; i++) {
|
|
232 var liElement = document.createElement('li');
|
|
233 Sys.Mvc._validationUtil.setInnerText(liElement, this._errors[i]);
|
|
234 this._validationSummaryULElement.appendChild(liElement);
|
|
235 }
|
|
236 }
|
|
237 Sys.UI.DomElement.removeCssClass(this._validationSummaryElement, Sys.Mvc.FormContext._validationSummaryValidCss);
|
|
238 Sys.UI.DomElement.addCssClass(this._validationSummaryElement, Sys.Mvc.FormContext._validationSummaryErrorCss);
|
|
239 }
|
|
240 },
|
|
241
|
|
242 _displaySuccess: function Sys_Mvc_FormContext$_displaySuccess() {
|
|
243 var validationSummaryElement = this._validationSummaryElement;
|
|
244 if (validationSummaryElement) {
|
|
245 var validationSummaryULElement = this._validationSummaryULElement;
|
|
246 if (validationSummaryULElement) {
|
|
247 validationSummaryULElement.innerHTML = '';
|
|
248 }
|
|
249 Sys.UI.DomElement.removeCssClass(validationSummaryElement, Sys.Mvc.FormContext._validationSummaryErrorCss);
|
|
250 Sys.UI.DomElement.addCssClass(validationSummaryElement, Sys.Mvc.FormContext._validationSummaryValidCss);
|
|
251 }
|
|
252 },
|
|
253
|
|
254 enableDynamicValidation: function Sys_Mvc_FormContext$enableDynamicValidation() {
|
|
255 Sys.UI.DomEvent.addHandler(this._formElement, 'click', this._onClickHandler);
|
|
256 Sys.UI.DomEvent.addHandler(this._formElement, 'submit', this._onSubmitHandler);
|
|
257 },
|
|
258
|
|
259 _findSubmitButton: function Sys_Mvc_FormContext$_findSubmitButton(element) {
|
|
260 /// <param name="element" type="Object" domElement="true">
|
|
261 /// </param>
|
|
262 /// <returns type="Object" domElement="true"></returns>
|
|
263 if (element.disabled) {
|
|
264 return null;
|
|
265 }
|
|
266 var tagName = element.tagName.toUpperCase();
|
|
267 var inputElement = element;
|
|
268 if (tagName === 'INPUT') {
|
|
269 var type = inputElement.type;
|
|
270 if (type === 'submit' || type === 'image') {
|
|
271 return inputElement;
|
|
272 }
|
|
273 }
|
|
274 else if ((tagName === 'BUTTON') && (inputElement.type === 'submit')) {
|
|
275 return inputElement;
|
|
276 }
|
|
277 return null;
|
|
278 },
|
|
279
|
|
280 _form_OnClick: function Sys_Mvc_FormContext$_form_OnClick(e) {
|
|
281 /// <param name="e" type="Sys.UI.DomEvent">
|
|
282 /// </param>
|
|
283 this._submitButtonClicked = this._findSubmitButton(e.target);
|
|
284 },
|
|
285
|
|
286 _form_OnSubmit: function Sys_Mvc_FormContext$_form_OnSubmit(e) {
|
|
287 /// <param name="e" type="Sys.UI.DomEvent">
|
|
288 /// </param>
|
|
289 var form = e.target;
|
|
290 var submitButton = this._submitButtonClicked;
|
|
291 if (submitButton && submitButton.disableValidation) {
|
|
292 return;
|
|
293 }
|
|
294 var errorMessages = this.validate('submit');
|
|
295 if (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(errorMessages)) {
|
|
296 e.preventDefault();
|
|
297 }
|
|
298 },
|
|
299
|
|
300 _onErrorCountChanged: function Sys_Mvc_FormContext$_onErrorCountChanged() {
|
|
301 if (!this._errors.length) {
|
|
302 this._displaySuccess();
|
|
303 }
|
|
304 else {
|
|
305 this._displayError();
|
|
306 }
|
|
307 },
|
|
308
|
|
309 validate: function Sys_Mvc_FormContext$validate(eventName) {
|
|
310 /// <param name="eventName" type="String">
|
|
311 /// </param>
|
|
312 /// <returns type="Array" elementType="String"></returns>
|
|
313 var fields = this.fields;
|
|
314 var errors = [];
|
|
315 for (var i = 0; i < fields.length; i++) {
|
|
316 var field = fields[i];
|
|
317 if (!field.elements[0].disabled) {
|
|
318 var thisErrors = field.validate(eventName);
|
|
319 if (thisErrors) {
|
|
320 Array.addRange(errors, thisErrors);
|
|
321 }
|
|
322 }
|
|
323 }
|
|
324 if (this.replaceValidationSummary) {
|
|
325 this.clearErrors();
|
|
326 this.addErrors(errors);
|
|
327 }
|
|
328 return errors;
|
|
329 }
|
|
330 }
|
|
331
|
|
332
|
|
333 ////////////////////////////////////////////////////////////////////////////////
|
|
334 // Sys.Mvc.FieldContext
|
|
335
|
|
336 Sys.Mvc.FieldContext = function Sys_Mvc_FieldContext(formContext) {
|
|
337 /// <param name="formContext" type="Sys.Mvc.FormContext">
|
|
338 /// </param>
|
|
339 /// <field name="_hasTextChangedTag" type="String" static="true">
|
|
340 /// </field>
|
|
341 /// <field name="_hasValidationFiredTag" type="String" static="true">
|
|
342 /// </field>
|
|
343 /// <field name="_inputElementErrorCss" type="String" static="true">
|
|
344 /// </field>
|
|
345 /// <field name="_inputElementValidCss" type="String" static="true">
|
|
346 /// </field>
|
|
347 /// <field name="_validationMessageErrorCss" type="String" static="true">
|
|
348 /// </field>
|
|
349 /// <field name="_validationMessageValidCss" type="String" static="true">
|
|
350 /// </field>
|
|
351 /// <field name="_onBlurHandler" type="Sys.UI.DomEventHandler">
|
|
352 /// </field>
|
|
353 /// <field name="_onChangeHandler" type="Sys.UI.DomEventHandler">
|
|
354 /// </field>
|
|
355 /// <field name="_onInputHandler" type="Sys.UI.DomEventHandler">
|
|
356 /// </field>
|
|
357 /// <field name="_onPropertyChangeHandler" type="Sys.UI.DomEventHandler">
|
|
358 /// </field>
|
|
359 /// <field name="_errors" type="Array">
|
|
360 /// </field>
|
|
361 /// <field name="defaultErrorMessage" type="String">
|
|
362 /// </field>
|
|
363 /// <field name="elements" type="Array" elementType="Object" elementDomElement="true">
|
|
364 /// </field>
|
|
365 /// <field name="formContext" type="Sys.Mvc.FormContext">
|
|
366 /// </field>
|
|
367 /// <field name="replaceValidationMessageContents" type="Boolean">
|
|
368 /// </field>
|
|
369 /// <field name="validationMessageElement" type="Object" domElement="true">
|
|
370 /// </field>
|
|
371 /// <field name="validations" type="Array" elementType="Validation">
|
|
372 /// </field>
|
|
373 this._errors = [];
|
|
374 this.elements = new Array(0);
|
|
375 this.validations = new Array(0);
|
|
376 this.formContext = formContext;
|
|
377 this._onBlurHandler = Function.createDelegate(this, this._element_OnBlur);
|
|
378 this._onChangeHandler = Function.createDelegate(this, this._element_OnChange);
|
|
379 this._onInputHandler = Function.createDelegate(this, this._element_OnInput);
|
|
380 this._onPropertyChangeHandler = Function.createDelegate(this, this._element_OnPropertyChange);
|
|
381 }
|
|
382 Sys.Mvc.FieldContext.prototype = {
|
|
383 _onBlurHandler: null,
|
|
384 _onChangeHandler: null,
|
|
385 _onInputHandler: null,
|
|
386 _onPropertyChangeHandler: null,
|
|
387 defaultErrorMessage: null,
|
|
388 formContext: null,
|
|
389 replaceValidationMessageContents: false,
|
|
390 validationMessageElement: null,
|
|
391
|
|
392 addError: function Sys_Mvc_FieldContext$addError(message) {
|
|
393 /// <param name="message" type="String">
|
|
394 /// </param>
|
|
395 this.addErrors([ message ]);
|
|
396 },
|
|
397
|
|
398 addErrors: function Sys_Mvc_FieldContext$addErrors(messages) {
|
|
399 /// <param name="messages" type="Array" elementType="String">
|
|
400 /// </param>
|
|
401 if (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(messages)) {
|
|
402 Array.addRange(this._errors, messages);
|
|
403 this._onErrorCountChanged();
|
|
404 }
|
|
405 },
|
|
406
|
|
407 clearErrors: function Sys_Mvc_FieldContext$clearErrors() {
|
|
408 Array.clear(this._errors);
|
|
409 this._onErrorCountChanged();
|
|
410 },
|
|
411
|
|
412 _displayError: function Sys_Mvc_FieldContext$_displayError() {
|
|
413 var validationMessageElement = this.validationMessageElement;
|
|
414 if (validationMessageElement) {
|
|
415 if (this.replaceValidationMessageContents) {
|
|
416 Sys.Mvc._validationUtil.setInnerText(validationMessageElement, this._errors[0]);
|
|
417 }
|
|
418 Sys.UI.DomElement.removeCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageValidCss);
|
|
419 Sys.UI.DomElement.addCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageErrorCss);
|
|
420 }
|
|
421 var elements = this.elements;
|
|
422 for (var i = 0; i < elements.length; i++) {
|
|
423 var element = elements[i];
|
|
424 Sys.UI.DomElement.removeCssClass(element, Sys.Mvc.FieldContext._inputElementValidCss);
|
|
425 Sys.UI.DomElement.addCssClass(element, Sys.Mvc.FieldContext._inputElementErrorCss);
|
|
426 }
|
|
427 },
|
|
428
|
|
429 _displaySuccess: function Sys_Mvc_FieldContext$_displaySuccess() {
|
|
430 var validationMessageElement = this.validationMessageElement;
|
|
431 if (validationMessageElement) {
|
|
432 if (this.replaceValidationMessageContents) {
|
|
433 Sys.Mvc._validationUtil.setInnerText(validationMessageElement, '');
|
|
434 }
|
|
435 Sys.UI.DomElement.removeCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageErrorCss);
|
|
436 Sys.UI.DomElement.addCssClass(validationMessageElement, Sys.Mvc.FieldContext._validationMessageValidCss);
|
|
437 }
|
|
438 var elements = this.elements;
|
|
439 for (var i = 0; i < elements.length; i++) {
|
|
440 var element = elements[i];
|
|
441 Sys.UI.DomElement.removeCssClass(element, Sys.Mvc.FieldContext._inputElementErrorCss);
|
|
442 Sys.UI.DomElement.addCssClass(element, Sys.Mvc.FieldContext._inputElementValidCss);
|
|
443 }
|
|
444 },
|
|
445
|
|
446 _element_OnBlur: function Sys_Mvc_FieldContext$_element_OnBlur(e) {
|
|
447 /// <param name="e" type="Sys.UI.DomEvent">
|
|
448 /// </param>
|
|
449 if (e.target[Sys.Mvc.FieldContext._hasTextChangedTag] || e.target[Sys.Mvc.FieldContext._hasValidationFiredTag]) {
|
|
450 this.validate('blur');
|
|
451 }
|
|
452 },
|
|
453
|
|
454 _element_OnChange: function Sys_Mvc_FieldContext$_element_OnChange(e) {
|
|
455 /// <param name="e" type="Sys.UI.DomEvent">
|
|
456 /// </param>
|
|
457 e.target[Sys.Mvc.FieldContext._hasTextChangedTag] = true;
|
|
458 },
|
|
459
|
|
460 _element_OnInput: function Sys_Mvc_FieldContext$_element_OnInput(e) {
|
|
461 /// <param name="e" type="Sys.UI.DomEvent">
|
|
462 /// </param>
|
|
463 e.target[Sys.Mvc.FieldContext._hasTextChangedTag] = true;
|
|
464 if (e.target[Sys.Mvc.FieldContext._hasValidationFiredTag]) {
|
|
465 this.validate('input');
|
|
466 }
|
|
467 },
|
|
468
|
|
469 _element_OnPropertyChange: function Sys_Mvc_FieldContext$_element_OnPropertyChange(e) {
|
|
470 /// <param name="e" type="Sys.UI.DomEvent">
|
|
471 /// </param>
|
|
472 if (e.rawEvent.propertyName === 'value') {
|
|
473 e.target[Sys.Mvc.FieldContext._hasTextChangedTag] = true;
|
|
474 if (e.target[Sys.Mvc.FieldContext._hasValidationFiredTag]) {
|
|
475 this.validate('input');
|
|
476 }
|
|
477 }
|
|
478 },
|
|
479
|
|
480 enableDynamicValidation: function Sys_Mvc_FieldContext$enableDynamicValidation() {
|
|
481 var elements = this.elements;
|
|
482 for (var i = 0; i < elements.length; i++) {
|
|
483 var element = elements[i];
|
|
484 if (Sys.Mvc._validationUtil.elementSupportsEvent(element, 'onpropertychange')) {
|
|
485 var compatMode = document.documentMode;
|
|
486 if (compatMode && compatMode >= 8) {
|
|
487 Sys.UI.DomEvent.addHandler(element, 'propertychange', this._onPropertyChangeHandler);
|
|
488 }
|
|
489 }
|
|
490 else {
|
|
491 Sys.UI.DomEvent.addHandler(element, 'input', this._onInputHandler);
|
|
492 }
|
|
493 Sys.UI.DomEvent.addHandler(element, 'change', this._onChangeHandler);
|
|
494 Sys.UI.DomEvent.addHandler(element, 'blur', this._onBlurHandler);
|
|
495 }
|
|
496 },
|
|
497
|
|
498 _getErrorString: function Sys_Mvc_FieldContext$_getErrorString(validatorReturnValue, fieldErrorMessage) {
|
|
499 /// <param name="validatorReturnValue" type="Object">
|
|
500 /// </param>
|
|
501 /// <param name="fieldErrorMessage" type="String">
|
|
502 /// </param>
|
|
503 /// <returns type="String"></returns>
|
|
504 var fallbackErrorMessage = fieldErrorMessage || this.defaultErrorMessage;
|
|
505 if (Boolean.isInstanceOfType(validatorReturnValue)) {
|
|
506 return (validatorReturnValue) ? null : fallbackErrorMessage;
|
|
507 }
|
|
508 if (String.isInstanceOfType(validatorReturnValue)) {
|
|
509 return ((validatorReturnValue).length) ? validatorReturnValue : fallbackErrorMessage;
|
|
510 }
|
|
511 return null;
|
|
512 },
|
|
513
|
|
514 _getStringValue: function Sys_Mvc_FieldContext$_getStringValue() {
|
|
515 /// <returns type="String"></returns>
|
|
516 var elements = this.elements;
|
|
517 return (elements.length > 0) ? elements[0].value : null;
|
|
518 },
|
|
519
|
|
520 _markValidationFired: function Sys_Mvc_FieldContext$_markValidationFired() {
|
|
521 var elements = this.elements;
|
|
522 for (var i = 0; i < elements.length; i++) {
|
|
523 var element = elements[i];
|
|
524 element[Sys.Mvc.FieldContext._hasValidationFiredTag] = true;
|
|
525 }
|
|
526 },
|
|
527
|
|
528 _onErrorCountChanged: function Sys_Mvc_FieldContext$_onErrorCountChanged() {
|
|
529 if (!this._errors.length) {
|
|
530 this._displaySuccess();
|
|
531 }
|
|
532 else {
|
|
533 this._displayError();
|
|
534 }
|
|
535 },
|
|
536
|
|
537 validate: function Sys_Mvc_FieldContext$validate(eventName) {
|
|
538 /// <param name="eventName" type="String">
|
|
539 /// </param>
|
|
540 /// <returns type="Array" elementType="String"></returns>
|
|
541 var validations = this.validations;
|
|
542 var errors = [];
|
|
543 var value = this._getStringValue();
|
|
544 for (var i = 0; i < validations.length; i++) {
|
|
545 var validation = validations[i];
|
|
546 var context = Sys.Mvc.$create_ValidationContext();
|
|
547 context.eventName = eventName;
|
|
548 context.fieldContext = this;
|
|
549 context.validation = validation;
|
|
550 var retVal = validation.validator(value, context);
|
|
551 var errorMessage = this._getErrorString(retVal, validation.fieldErrorMessage);
|
|
552 if (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(errorMessage)) {
|
|
553 Array.add(errors, errorMessage);
|
|
554 }
|
|
555 }
|
|
556 this._markValidationFired();
|
|
557 this.clearErrors();
|
|
558 this.addErrors(errors);
|
|
559 return errors;
|
|
560 }
|
|
561 }
|
|
562
|
|
563
|
|
564 ////////////////////////////////////////////////////////////////////////////////
|
|
565 // Sys.Mvc.RangeValidator
|
|
566
|
|
567 Sys.Mvc.RangeValidator = function Sys_Mvc_RangeValidator(minimum, maximum) {
|
|
568 /// <param name="minimum" type="Number">
|
|
569 /// </param>
|
|
570 /// <param name="maximum" type="Number">
|
|
571 /// </param>
|
|
572 /// <field name="_minimum" type="Number">
|
|
573 /// </field>
|
|
574 /// <field name="_maximum" type="Number">
|
|
575 /// </field>
|
|
576 this._minimum = minimum;
|
|
577 this._maximum = maximum;
|
|
578 }
|
|
579 Sys.Mvc.RangeValidator.create = function Sys_Mvc_RangeValidator$create(rule) {
|
|
580 /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
|
|
581 /// </param>
|
|
582 /// <returns type="Sys.Mvc.Validator"></returns>
|
|
583 var min = rule.ValidationParameters['min'];
|
|
584 var max = rule.ValidationParameters['max'];
|
|
585 return Function.createDelegate(new Sys.Mvc.RangeValidator(min, max), new Sys.Mvc.RangeValidator(min, max).validate);
|
|
586 }
|
|
587 Sys.Mvc.RangeValidator.prototype = {
|
|
588 _minimum: null,
|
|
589 _maximum: null,
|
|
590
|
|
591 validate: function Sys_Mvc_RangeValidator$validate(value, context) {
|
|
592 /// <param name="value" type="String">
|
|
593 /// </param>
|
|
594 /// <param name="context" type="Sys.Mvc.ValidationContext">
|
|
595 /// </param>
|
|
596 /// <returns type="Object"></returns>
|
|
597 if (Sys.Mvc._validationUtil.stringIsNullOrEmpty(value)) {
|
|
598 return true;
|
|
599 }
|
|
600 var n = Number.parseLocale(value);
|
|
601 return (!isNaN(n) && this._minimum <= n && n <= this._maximum);
|
|
602 }
|
|
603 }
|
|
604
|
|
605
|
|
606 ////////////////////////////////////////////////////////////////////////////////
|
|
607 // Sys.Mvc.RegularExpressionValidator
|
|
608
|
|
609 Sys.Mvc.RegularExpressionValidator = function Sys_Mvc_RegularExpressionValidator(pattern) {
|
|
610 /// <param name="pattern" type="String">
|
|
611 /// </param>
|
|
612 /// <field name="_pattern" type="String">
|
|
613 /// </field>
|
|
614 this._pattern = pattern;
|
|
615 }
|
|
616 Sys.Mvc.RegularExpressionValidator.create = function Sys_Mvc_RegularExpressionValidator$create(rule) {
|
|
617 /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
|
|
618 /// </param>
|
|
619 /// <returns type="Sys.Mvc.Validator"></returns>
|
|
620 var pattern = rule.ValidationParameters['pattern'];
|
|
621 return Function.createDelegate(new Sys.Mvc.RegularExpressionValidator(pattern), new Sys.Mvc.RegularExpressionValidator(pattern).validate);
|
|
622 }
|
|
623 Sys.Mvc.RegularExpressionValidator.prototype = {
|
|
624 _pattern: null,
|
|
625
|
|
626 validate: function Sys_Mvc_RegularExpressionValidator$validate(value, context) {
|
|
627 /// <param name="value" type="String">
|
|
628 /// </param>
|
|
629 /// <param name="context" type="Sys.Mvc.ValidationContext">
|
|
630 /// </param>
|
|
631 /// <returns type="Object"></returns>
|
|
632 if (Sys.Mvc._validationUtil.stringIsNullOrEmpty(value)) {
|
|
633 return true;
|
|
634 }
|
|
635 var regExp = new RegExp(this._pattern);
|
|
636 var matches = regExp.exec(value);
|
|
637 return (!Sys.Mvc._validationUtil.arrayIsNullOrEmpty(matches) && matches[0].length === value.length);
|
|
638 }
|
|
639 }
|
|
640
|
|
641
|
|
642 ////////////////////////////////////////////////////////////////////////////////
|
|
643 // Sys.Mvc.RequiredValidator
|
|
644
|
|
645 Sys.Mvc.RequiredValidator = function Sys_Mvc_RequiredValidator() {
|
|
646 }
|
|
647 Sys.Mvc.RequiredValidator.create = function Sys_Mvc_RequiredValidator$create(rule) {
|
|
648 /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
|
|
649 /// </param>
|
|
650 /// <returns type="Sys.Mvc.Validator"></returns>
|
|
651 return Function.createDelegate(new Sys.Mvc.RequiredValidator(), new Sys.Mvc.RequiredValidator().validate);
|
|
652 }
|
|
653 Sys.Mvc.RequiredValidator._isRadioInputElement = function Sys_Mvc_RequiredValidator$_isRadioInputElement(element) {
|
|
654 /// <param name="element" type="Object" domElement="true">
|
|
655 /// </param>
|
|
656 /// <returns type="Boolean"></returns>
|
|
657 if (element.tagName.toUpperCase() === 'INPUT') {
|
|
658 var inputType = (element.type).toUpperCase();
|
|
659 if (inputType === 'RADIO') {
|
|
660 return true;
|
|
661 }
|
|
662 }
|
|
663 return false;
|
|
664 }
|
|
665 Sys.Mvc.RequiredValidator._isSelectInputElement = function Sys_Mvc_RequiredValidator$_isSelectInputElement(element) {
|
|
666 /// <param name="element" type="Object" domElement="true">
|
|
667 /// </param>
|
|
668 /// <returns type="Boolean"></returns>
|
|
669 if (element.tagName.toUpperCase() === 'SELECT') {
|
|
670 return true;
|
|
671 }
|
|
672 return false;
|
|
673 }
|
|
674 Sys.Mvc.RequiredValidator._isTextualInputElement = function Sys_Mvc_RequiredValidator$_isTextualInputElement(element) {
|
|
675 /// <param name="element" type="Object" domElement="true">
|
|
676 /// </param>
|
|
677 /// <returns type="Boolean"></returns>
|
|
678 if (element.tagName.toUpperCase() === 'INPUT') {
|
|
679 var inputType = (element.type).toUpperCase();
|
|
680 switch (inputType) {
|
|
681 case 'TEXT':
|
|
682 case 'PASSWORD':
|
|
683 case 'FILE':
|
|
684 return true;
|
|
685 }
|
|
686 }
|
|
687 if (element.tagName.toUpperCase() === 'TEXTAREA') {
|
|
688 return true;
|
|
689 }
|
|
690 return false;
|
|
691 }
|
|
692 Sys.Mvc.RequiredValidator._validateRadioInput = function Sys_Mvc_RequiredValidator$_validateRadioInput(elements) {
|
|
693 /// <param name="elements" type="Array" elementType="Object" elementDomElement="true">
|
|
694 /// </param>
|
|
695 /// <returns type="Object"></returns>
|
|
696 for (var i = 0; i < elements.length; i++) {
|
|
697 var element = elements[i];
|
|
698 if (element.checked) {
|
|
699 return true;
|
|
700 }
|
|
701 }
|
|
702 return false;
|
|
703 }
|
|
704 Sys.Mvc.RequiredValidator._validateSelectInput = function Sys_Mvc_RequiredValidator$_validateSelectInput(optionElements) {
|
|
705 /// <param name="optionElements" type="DOMElementCollection">
|
|
706 /// </param>
|
|
707 /// <returns type="Object"></returns>
|
|
708 for (var i = 0; i < optionElements.length; i++) {
|
|
709 var element = optionElements[i];
|
|
710 if (element.selected) {
|
|
711 if (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(element.value)) {
|
|
712 return true;
|
|
713 }
|
|
714 }
|
|
715 }
|
|
716 return false;
|
|
717 }
|
|
718 Sys.Mvc.RequiredValidator._validateTextualInput = function Sys_Mvc_RequiredValidator$_validateTextualInput(element) {
|
|
719 /// <param name="element" type="Object" domElement="true">
|
|
720 /// </param>
|
|
721 /// <returns type="Object"></returns>
|
|
722 return (!Sys.Mvc._validationUtil.stringIsNullOrEmpty(element.value));
|
|
723 }
|
|
724 Sys.Mvc.RequiredValidator.prototype = {
|
|
725
|
|
726 validate: function Sys_Mvc_RequiredValidator$validate(value, context) {
|
|
727 /// <param name="value" type="String">
|
|
728 /// </param>
|
|
729 /// <param name="context" type="Sys.Mvc.ValidationContext">
|
|
730 /// </param>
|
|
731 /// <returns type="Object"></returns>
|
|
732 var elements = context.fieldContext.elements;
|
|
733 if (!elements.length) {
|
|
734 return true;
|
|
735 }
|
|
736 var sampleElement = elements[0];
|
|
737 if (Sys.Mvc.RequiredValidator._isTextualInputElement(sampleElement)) {
|
|
738 return Sys.Mvc.RequiredValidator._validateTextualInput(sampleElement);
|
|
739 }
|
|
740 if (Sys.Mvc.RequiredValidator._isRadioInputElement(sampleElement)) {
|
|
741 return Sys.Mvc.RequiredValidator._validateRadioInput(elements);
|
|
742 }
|
|
743 if (Sys.Mvc.RequiredValidator._isSelectInputElement(sampleElement)) {
|
|
744 return Sys.Mvc.RequiredValidator._validateSelectInput((sampleElement).options);
|
|
745 }
|
|
746 return true;
|
|
747 }
|
|
748 }
|
|
749
|
|
750
|
|
751 ////////////////////////////////////////////////////////////////////////////////
|
|
752 // Sys.Mvc.StringLengthValidator
|
|
753
|
|
754 Sys.Mvc.StringLengthValidator = function Sys_Mvc_StringLengthValidator(minLength, maxLength) {
|
|
755 /// <param name="minLength" type="Number" integer="true">
|
|
756 /// </param>
|
|
757 /// <param name="maxLength" type="Number" integer="true">
|
|
758 /// </param>
|
|
759 /// <field name="_maxLength" type="Number" integer="true">
|
|
760 /// </field>
|
|
761 /// <field name="_minLength" type="Number" integer="true">
|
|
762 /// </field>
|
|
763 this._minLength = minLength;
|
|
764 this._maxLength = maxLength;
|
|
765 }
|
|
766 Sys.Mvc.StringLengthValidator.create = function Sys_Mvc_StringLengthValidator$create(rule) {
|
|
767 /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
|
|
768 /// </param>
|
|
769 /// <returns type="Sys.Mvc.Validator"></returns>
|
|
770 var minLength = (rule.ValidationParameters['min'] || 0);
|
|
771 var maxLength = (rule.ValidationParameters['max'] || Number.MAX_VALUE);
|
|
772 return Function.createDelegate(new Sys.Mvc.StringLengthValidator(minLength, maxLength), new Sys.Mvc.StringLengthValidator(minLength, maxLength).validate);
|
|
773 }
|
|
774 Sys.Mvc.StringLengthValidator.prototype = {
|
|
775 _maxLength: 0,
|
|
776 _minLength: 0,
|
|
777
|
|
778 validate: function Sys_Mvc_StringLengthValidator$validate(value, context) {
|
|
779 /// <param name="value" type="String">
|
|
780 /// </param>
|
|
781 /// <param name="context" type="Sys.Mvc.ValidationContext">
|
|
782 /// </param>
|
|
783 /// <returns type="Object"></returns>
|
|
784 if (Sys.Mvc._validationUtil.stringIsNullOrEmpty(value)) {
|
|
785 return true;
|
|
786 }
|
|
787 return (this._minLength <= value.length && value.length <= this._maxLength);
|
|
788 }
|
|
789 }
|
|
790
|
|
791
|
|
792 ////////////////////////////////////////////////////////////////////////////////
|
|
793 // Sys.Mvc._validationUtil
|
|
794
|
|
795 Sys.Mvc._validationUtil = function Sys_Mvc__validationUtil() {
|
|
796 }
|
|
797 Sys.Mvc._validationUtil.arrayIsNullOrEmpty = function Sys_Mvc__validationUtil$arrayIsNullOrEmpty(array) {
|
|
798 /// <param name="array" type="Array" elementType="Object">
|
|
799 /// </param>
|
|
800 /// <returns type="Boolean"></returns>
|
|
801 return (!array || !array.length);
|
|
802 }
|
|
803 Sys.Mvc._validationUtil.stringIsNullOrEmpty = function Sys_Mvc__validationUtil$stringIsNullOrEmpty(value) {
|
|
804 /// <param name="value" type="String">
|
|
805 /// </param>
|
|
806 /// <returns type="Boolean"></returns>
|
|
807 return (!value || !value.length);
|
|
808 }
|
|
809 Sys.Mvc._validationUtil.elementSupportsEvent = function Sys_Mvc__validationUtil$elementSupportsEvent(element, eventAttributeName) {
|
|
810 /// <param name="element" type="Object" domElement="true">
|
|
811 /// </param>
|
|
812 /// <param name="eventAttributeName" type="String">
|
|
813 /// </param>
|
|
814 /// <returns type="Boolean"></returns>
|
|
815 return (eventAttributeName in element);
|
|
816 }
|
|
817 Sys.Mvc._validationUtil.removeAllChildren = function Sys_Mvc__validationUtil$removeAllChildren(element) {
|
|
818 /// <param name="element" type="Object" domElement="true">
|
|
819 /// </param>
|
|
820 while (element.firstChild) {
|
|
821 element.removeChild(element.firstChild);
|
|
822 }
|
|
823 }
|
|
824 Sys.Mvc._validationUtil.setInnerText = function Sys_Mvc__validationUtil$setInnerText(element, innerText) {
|
|
825 /// <param name="element" type="Object" domElement="true">
|
|
826 /// </param>
|
|
827 /// <param name="innerText" type="String">
|
|
828 /// </param>
|
|
829 var textNode = document.createTextNode(innerText);
|
|
830 Sys.Mvc._validationUtil.removeAllChildren(element);
|
|
831 element.appendChild(textNode);
|
|
832 }
|
|
833
|
|
834
|
|
835 ////////////////////////////////////////////////////////////////////////////////
|
|
836 // Sys.Mvc.ValidatorRegistry
|
|
837
|
|
838 Sys.Mvc.ValidatorRegistry = function Sys_Mvc_ValidatorRegistry() {
|
|
839 /// <field name="validators" type="Object" static="true">
|
|
840 /// </field>
|
|
841 }
|
|
842 Sys.Mvc.ValidatorRegistry.getValidator = function Sys_Mvc_ValidatorRegistry$getValidator(rule) {
|
|
843 /// <param name="rule" type="Sys.Mvc.JsonValidationRule">
|
|
844 /// </param>
|
|
845 /// <returns type="Sys.Mvc.Validator"></returns>
|
|
846 var creator = Sys.Mvc.ValidatorRegistry.validators[rule.ValidationType];
|
|
847 return (creator) ? creator(rule) : null;
|
|
848 }
|
|
849 Sys.Mvc.ValidatorRegistry._getDefaultValidators = function Sys_Mvc_ValidatorRegistry$_getDefaultValidators() {
|
|
850 /// <returns type="Object"></returns>
|
|
851 return { required: Function.createDelegate(null, Sys.Mvc.RequiredValidator.create), length: Function.createDelegate(null, Sys.Mvc.StringLengthValidator.create), regex: Function.createDelegate(null, Sys.Mvc.RegularExpressionValidator.create), range: Function.createDelegate(null, Sys.Mvc.RangeValidator.create), number: Function.createDelegate(null, Sys.Mvc.NumberValidator.create) };
|
|
852 }
|
|
853
|
|
854
|
|
855 Sys.Mvc.NumberValidator.registerClass('Sys.Mvc.NumberValidator');
|
|
856 Sys.Mvc.FormContext.registerClass('Sys.Mvc.FormContext');
|
|
857 Sys.Mvc.FieldContext.registerClass('Sys.Mvc.FieldContext');
|
|
858 Sys.Mvc.RangeValidator.registerClass('Sys.Mvc.RangeValidator');
|
|
859 Sys.Mvc.RegularExpressionValidator.registerClass('Sys.Mvc.RegularExpressionValidator');
|
|
860 Sys.Mvc.RequiredValidator.registerClass('Sys.Mvc.RequiredValidator');
|
|
861 Sys.Mvc.StringLengthValidator.registerClass('Sys.Mvc.StringLengthValidator');
|
|
862 Sys.Mvc._validationUtil.registerClass('Sys.Mvc._validationUtil');
|
|
863 Sys.Mvc.ValidatorRegistry.registerClass('Sys.Mvc.ValidatorRegistry');
|
|
864 Sys.Mvc.FormContext._validationSummaryErrorCss = 'validation-summary-errors';
|
|
865 Sys.Mvc.FormContext._validationSummaryValidCss = 'validation-summary-valid';
|
|
866 Sys.Mvc.FormContext._formValidationTag = '__MVC_FormValidation';
|
|
867 Sys.Mvc.FieldContext._hasTextChangedTag = '__MVC_HasTextChanged';
|
|
868 Sys.Mvc.FieldContext._hasValidationFiredTag = '__MVC_HasValidationFired';
|
|
869 Sys.Mvc.FieldContext._inputElementErrorCss = 'input-validation-error';
|
|
870 Sys.Mvc.FieldContext._inputElementValidCss = 'input-validation-valid';
|
|
871 Sys.Mvc.FieldContext._validationMessageErrorCss = 'field-validation-error';
|
|
872 Sys.Mvc.FieldContext._validationMessageValidCss = 'field-validation-valid';
|
|
873 Sys.Mvc.ValidatorRegistry.validators = Sys.Mvc.ValidatorRegistry._getDefaultValidators();
|
|
874
|
|
875 // ---- Do not remove this footer ----
|
|
876 // Generated using Script# v0.5.0.0 (http://projects.nikhilk.net)
|
|
877 // -----------------------------------
|
|
878
|
|
879 // register validation
|
|
880 Sys.Application.add_load(function() {
|
|
881 Sys.Application.remove_load(arguments.callee);
|
|
882 Sys.Mvc.FormContext._Application_Load();
|
|
883 });
|