1/** 2 * Yii form widget. 3 * 4 * This is the JavaScript widget used by the yii\widgets\ActiveForm widget. 5 * 6 * @link http://www.yiiframework.com/ 7 * @copyright Copyright (c) 2008 Yii Software LLC 8 * @license http://www.yiiframework.com/license/ 9 * @author Qiang Xue <[email protected]> 10 * @since 2.0 11 */ 12(function ($) { 13 14 $.fn.yiiActiveForm = function (method) { 15 if (methods[method]) { 16 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 17 } else { 18 if (typeof method === 'object' || !method) { 19 return methods.init.apply(this, arguments); 20 } else { 21 $.error('Method ' + method + ' does not exist on jQuery.yiiActiveForm'); 22 return false; 23 } 24 } 25 }; 26 27 var events = { 28 /** 29 * beforeValidate event is triggered before validating the whole form. 30 * The signature of the event handler should be: 31 * function (event, messages, deferreds) 32 * where 33 * - event: an Event object. 34 * - messages: an associative array with keys being attribute IDs and values being error message arrays 35 * for the corresponding attributes. 36 * - deferreds: an array of Deferred objects. You can use deferreds.add(callback) to add a new deferred validation. 37 * 38 * If the handler returns a boolean false, it will stop further form validation after this event. And as 39 * a result, afterValidate event will not be triggered. 40 */ 41 beforeValidate: 'beforeValidate', 42 /** 43 * afterValidate event is triggered after validating the whole form. 44 * The signature of the event handler should be: 45 * function (event, messages, errorAttributes) 46 * where 47 * - event: an Event object. 48 * - messages: an associative array with keys being attribute IDs and values being error message arrays 49 * for the corresponding attributes. 50 * - errorAttributes: an array of attributes that have validation errors. Please refer to attributeDefaults for the structure of this parameter. 51 */ 52 afterValidate: 'afterValidate', 53 /** 54 * beforeValidateAttribute event is triggered before validating an attribute. 55 * The signature of the event handler should be: 56 * function (event, attribute, messages, deferreds) 57 * where 58 * - event: an Event object. 59 * - attribute: the attribute to be validated. Please refer to attributeDefaults for the structure of this parameter. 60 * - messages: an array to which you can add validation error messages for the specified attribute. 61 * - deferreds: an array of Deferred objects. You can use deferreds.add(callback) to add a new deferred validation. 62 * 63 * If the handler returns a boolean false, it will stop further validation of the specified attribute. 64 * And as a result, afterValidateAttribute event will not be triggered. 65 */ 66 beforeValidateAttribute: 'beforeValidateAttribute', 67 /** 68 * afterValidateAttribute event is triggered after validating the whole form and each attribute. 69 * The signature of the event handler should be: 70 * function (event, attribute, messages) 71 * where 72 * - event: an Event object. 73 * - attribute: the attribute being validated. Please refer to attributeDefaults for the structure of this parameter. 74 * - messages: an array to which you can add additional validation error messages for the specified attribute. 75 */ 76 afterValidateAttribute: 'afterValidateAttribute', 77 /** 78 * beforeSubmit event is triggered before submitting the form after all validations have passed. 79 * The signature of the event handler should be: 80 * function (event) 81 * where event is an Event object. 82 * 83 * If the handler returns a boolean false, it will stop form submission. 84 */ 85 beforeSubmit: 'beforeSubmit', 86 /** 87 * ajaxBeforeSend event is triggered before sending an AJAX request for AJAX-based validation. 88 * The signature of the event handler should be: 89 * function (event, jqXHR, settings) 90 * where 91 * - event: an Event object. 92 * - jqXHR: a jqXHR object 93 * - settings: the settings for the AJAX request 94 */ 95 ajaxBeforeSend: 'ajaxBeforeSend', 96 /** 97 * ajaxComplete event is triggered after completing an AJAX request for AJAX-based validation. 98 * The signature of the event handler should be: 99 * function (event, jqXHR, textStatus) 100 * where 101 * - event: an Event object. 102 * - jqXHR: a jqXHR object 103 * - textStatus: the status of the request ("success", "notmodified", "error", "timeout", "abort", or "parsererror"). 104 */ 105 ajaxComplete: 'ajaxComplete', 106 /** 107 * afterInit event is triggered after yii activeForm init. 108 * The signature of the event handler should be: 109 * function (event) 110 * where 111 * - event: an Event object. 112 */ 113 afterInit: 'afterInit' 114 }; 115 116 // NOTE: If you change any of these defaults, make sure you update yii\widgets\ActiveForm::getClientOptions() as well 117 var defaults = { 118 // whether to encode the error summary 119 encodeErrorSummary: true, 120 // the jQuery selector for the error summary 121 errorSummary: '.error-summary', 122 // whether to perform validation before submitting the form. 123 validateOnSubmit: true, 124 // the container CSS class representing the corresponding attribute has validation error 125 errorCssClass: 'has-error', 126 // the container CSS class representing the corresponding attribute passes validation 127 successCssClass: 'has-success', 128 // the container CSS class representing the corresponding attribute is being validated 129 validatingCssClass: 'validating', 130 // the GET parameter name indicating an AJAX-based validation 131 ajaxParam: 'ajax', 132 // the type of data that you're expecting back from the server 133 ajaxDataType: 'json', 134 // the URL for performing AJAX-based validation. If not set, it will use the the form's action 135 validationUrl: undefined, 136 // whether to scroll to first visible error after validation. 137 scrollToError: true, 138 // offset in pixels that should be added when scrolling to the first error. 139 scrollToErrorOffset: 0, 140 // where to add validation class: container or input 141 validationStateOn: 'container' 142 }; 143 144 // NOTE: If you change any of these defaults, make sure you update yii\widgets\ActiveField::getClientOptions() as well 145 var attributeDefaults = { 146 // a unique ID identifying an attribute (e.g. "loginform-username") in a form 147 id: undefined, 148 // attribute name or expression (e.g. "[0]content" for tabular input) 149 name: undefined, 150 // the jQuery selector of the container of the input field 151 container: undefined, 152 // the jQuery selector of the input field under the context of the form 153 input: undefined, 154 // the jQuery selector of the error tag under the context of the container 155 error: '.help-block', 156 // whether to encode the error 157 encodeError: true, 158 // whether to perform validation when a change is detected on the input 159 validateOnChange: true, 160 // whether to perform validation when the input loses focus 161 validateOnBlur: true, 162 // whether to perform validation when the user is typing. 163 validateOnType: false, 164 // number of milliseconds that the validation should be delayed when a user is typing in the input field. 165 validationDelay: 500, 166 // whether to enable AJAX-based validation. 167 enableAjaxValidation: false, 168 // function (attribute, value, messages, deferred, $form), the client-side validation function. 169 validate: undefined, 170 // status of the input field, 0: empty, not entered before, 1: validated, 2: pending validation, 3: validating 171 status: 0, 172 // whether the validation is cancelled by beforeValidateAttribute event handler 173 cancelled: false, 174 // the value of the input 175 value: undefined, 176 // whether to update aria-invalid attribute after validation 177 updateAriaInvalid: true 178 }; 179 180 181 var submitDefer; 182 183 var setSubmitFinalizeDefer = function ($form) { 184 submitDefer = $.Deferred(); 185 $form.data('yiiSubmitFinalizePromise', submitDefer.promise()); 186 }; 187 188 // finalize yii.js $form.submit 189 var submitFinalize = function ($form) { 190 if (submitDefer) { 191 submitDefer.resolve(); 192 submitDefer = undefined; 193 $form.removeData('yiiSubmitFinalizePromise'); 194 } 195 }; 196 197 198 var methods = { 199 init: function (attributes, options) { 200 return this.each(function () { 201 var $form = $(this); 202 if ($form.data('yiiActiveForm')) { 203 return; 204 } 205 206 var settings = $.extend({}, defaults, options || {});
207 if (settings.validationUrl === undefined) { 208 settings.validationUrl = $form.attr('action'); 209 } 210 211 $.each(attributes, function (i) { 212 attributes[i] = $.extend({value: getValue($form, this)}, attributeDefaults, this); 213 watchAttribute($form, attributes[i]); 214 }); 215 216 $form.data('yiiActiveForm', { 217 settings: settings, 218 attributes: attributes, 219 submitting: false, 220 validated: false, 221 options: getFormOptions($form) 222 }); 223 224 /** 225 * Clean up error status when the form is reset. 226 * Note that $form.on('reset', ...) does work because the "reset" event does not bubble on IE. 227 */ 228 $form.on('reset.yiiActiveForm', methods.resetForm); 229 230 if (settings.validateOnSubmit) { 231 $form.on('mouseup.yiiActiveForm keyup.yiiActiveForm', ':submit', function () { 232 $form.data('yiiActiveForm').submitObject = $(this); 233 }); 234 $form.on('submit.yiiActiveForm', methods.submitForm); 235 } 236 var event = $.Event(events.afterInit); 237 $form.trigger(event); 238 }); 239 }, 240 241 // add a new attribute to the form dynamically. 242 // please refer to attributeDefaults for the structure of attribute 243 add: function (attribute) { 244 var $form = $(this); 245 attribute = $.extend({value: getValue($form, attribute)}, attributeDefaults, attribute); 246 $form.data('yiiActiveForm').attributes.push(attribute); 247 watchAttribute($form, attribute); 248 }, 249 250 // remove the attribute with the specified ID from the form 251 remove: function (id) { 252 var $form = $(this), 253 attributes = $form.data('yiiActiveForm').attributes, 254 index = -1, 255 attribute = undefined; 256 $.each(attributes, function (i) { 257 if (attributes[i]['id'] == id) { 258 index = i; 259 attribute = attributes[i]; 260 return false; 261 } 262 }); 263 if (index >= 0) { 264 attributes.splice(index, 1); 265 unwatchAttribute($form, attribute); 266 } 267 268 return attribute; 269 }, 270 271 // manually trigger the validation of the attribute with the specified ID 272 validateAttribute: function (id) { 273 var attribute = methods.find.call(this, id); 274 if (attribute != undefined) { 275 validateAttribute($(this), attribute, true); 276 } 277 }, 278 279 // find an attribute config based on the specified attribute ID 280 find: function (id) { 281 var attributes = $(this).data('yiiActiveForm').attributes, 282 result = undefined; 283 $.each(attributes, function (i) { 284 if (attributes[i]['id'] == id) { 285 result = attributes[i]; 286 return false; 287 } 288 }); 289 return result; 290 }, 291 292 destroy: function () { 293 return this.each(function () { 294 $(this).off('.yiiActiveForm'); 295 $(this).removeData('yiiActiveForm'); 296 }); 297 }, 298 299 data: function () { 300 return this.data('yiiActiveForm'); 301 }, 302 303 // validate all applicable inputs in the form 304 validate: function (forceValidate) { 305 if (forceValidate) { 306 $(this).data('yiiActiveForm').submitting = true; 307 } 308 309 var $form = $(this), 310 data = $form.data('yiiActiveForm'), 311 needAjaxValidation = false, 312 messages = {}, 313 deferreds = deferredArray(), 314 submitting = data.submitting; 315 316 if (submitting) { 317 var event = $.Event(events.beforeValidate); 318 $form.trigger(event, [messages, deferreds]); 319 320 if (event.result === false) { 321 data.submitting = false;
322 submitFinalize($form); 323 return; 324 } 325 } 326 327 // client-side validation 328 $.each(data.attributes, function () { 329 this.$form = $form; 330 var $input = findInput($form, this); 331 332 var disabled = $input.toArray().reduce(function(result, next) { 333 return result && $(next).is(':disabled'); 334 }, true); 335 if (disabled) { 336 return true; 337 } 338 // validate markup for select input 339 if ($input.length && $input[0].tagName.toLowerCase() === 'select') { 340 var opts = $input[0].options, isEmpty = !opts || !opts.length, isRequired = $input.attr('required'), 341 isMultiple = $input.attr('multiple'), size = $input.attr('size') || 1; 342 // check if valid HTML markup for select input, else return validation as `true` 343 // https://w3c.github.io/html-reference/select.html 344 if (isRequired && !isMultiple && parseInt(size, 10) === 1) { // invalid select markup condition 345 if (isEmpty) { // empty option elements for the select 346 return true; 347 } 348 if (opts[0] && (opts[0].value !== '' && opts[0].text !== '')) { // first option is not empty 349 return true; 350 } 351 } 352 } 353 this.cancelled = false; 354 // perform validation only if the form is being submitted or if an attribute is pending validation 355 if (data.submitting || this.status === 2 || this.status === 3) { 356 var msg = messages[this.id]; 357 if (msg === undefined) { 358 msg = []; 359 messages[this.id] = msg; 360 } 361 362 var event = $.Event(events.beforeValidateAttribute); 363 $form.trigger(event, [this, msg, deferreds]); 364 if (event.result !== false) { 365 if (this.validate) { 366 this.validate(this, getValue($form, this), msg, deferreds, $form); 367 } 368 if (this.enableAjaxValidation) { 369 needAjaxValidation = true; 370 } 371 } else { 372 this.cancelled = true; 373 } 374 } 375 }); 376 377 // ajax validation 378 $.when.apply(this, deferreds).always(function () { 379 // Remove empty message arrays 380 for (var i in messages) { 381 if (0 === messages[i].length) { 382 delete messages[i]; 383 } 384 } 385 if (needAjaxValidation && ($.isEmptyObject(messages) || data.submitting)) { 386 var $button = data.submitObject, 387 extData = '&' + data.settings.ajaxParam + '=' + $form.attr('id'); 388 if ($button && $button.length && $button.attr('name')) { 389 extData += '&' + $button.attr('name') + '=' + $button.attr('value'); 390 } 391 $.ajax({ 392 url: data.settings.validationUrl, 393 type: $form.attr('method'), 394 data: $form.serialize() + extData, 395 dataType: data.settings.ajaxDataType, 396 complete: function (jqXHR, textStatus) { 397 $form.trigger(events.ajaxComplete, [jqXHR, textStatus]); 398 }, 399 beforeSend: function (jqXHR, settings) { 400 $form.trigger(events.ajaxBeforeSend, [jqXHR, settings]); 401 }, 402 success: function (msgs) { 403 if (msgs !== null && typeof msgs === 'object') { 404 $.each(data.attributes, function () { 405 if (!this.enableAjaxValidation || this.cancelled) { 406 delete msgs[this.id]; 407 } 408 }); 409 updateInputs($form, $.extend(messages, msgs), submitting); 410 } else { 411 updateInputs($form, messages, submitting); 412 } 413 }, 414 error: function () { 415 data.submitting = false;
416 submitFinalize($form); 417 } 418 }); 419 } else { 420 if (data.submitting) { 421 // delay callback so that the form can be submitted without problem 422 window.setTimeout(function () { 423 updateInputs($form, messages, submitting); 424 }, 200); 425 } else { 426 updateInputs($form, messages, submitting); 427 } 428 } 429 }); 430 }, 431 432 submitForm: function () { 433 var $form = $(this), 434 data = $form.data('yiiActiveForm'); 435 if (data.validated) { 436 // Second submit's call (from validate/updateInputs) 437 data.submitting = false; 438 var event = $.Event(events.beforeSubmit); 439 $form.trigger(event); 440 if (event.result === false) { 441 data.validated = false; 442 submitFinalize($form); 443 return false; 444 } 445 updateHiddenButton($form); 446 return true; // continue submitting the form since validation passes 447 } else { 448 // First submit's call (from yii.js/handleAction) - execute validating 449 setSubmitFinalizeDefer($form); 450 451 if (data.settings.timer !== undefined) { 452 clearTimeout(data.settings.timer); 453 } 454 data.submitting = true; 455 methods.validate.call($form); 456 return false; 457 } 458 }, 459 460 resetForm: function () { 461 var $form = $(this); 462 var data = $form.data('yiiActiveForm'); 463 // Because we bind directly to a form reset event instead of a reset button (that may not exist), 464 // when this function is executed form input values have not been reset yet. 465 // Therefore we do the actual reset work through setTimeout. 466 window.setTimeout(function () { 467 $.each(data.attributes, function () {
468 // Without setTimeout() we would get the input values that are not reset yet. 469 this.value = getValue($form, this); 470 this.status = 0; 471 var $container = $form.find(this.container), 472 $input = findInput($form, this), 473 $errorElement = data.settings.validationStateOn === 'input' ? $input : $container; 474 475 $errorElement.removeClass( 476 data.settings.validatingCssClass + ' ' + 477 data.settings.errorCssClass + ' ' + 478 data.settings.successCssClass 479 ); 480 $container.find(this.error).html(''); 481 }); 482 $form.find(data.settings.errorSummary).hide().find('ul').html(''); 483 }, 1); 484 }, 485 486 /** 487 * Updates error messages, input containers, and optionally summary as well. 488 * If an attribute is missing from messages, it is considered valid. 489 * @param messages array the validation error messages, indexed by attribute IDs 490 * @param summary whether to update summary as well. 491 */ 492 updateMessages: function (messages, summary) { 493 var $form = $(this); 494 var data = $form.data('yiiActiveForm'); 495 $.each(data.attributes, function () { 496 updateInput($form, this, messages); 497 }); 498 if (summary) { 499 updateSummary($form, messages); 500 } 501 }, 502 503 /** 504 * Updates error messages and input container of a single attribute. 505 * If messages is empty, the attribute is considered valid. 506 * @param id attribute ID 507 * @param messages array with error messages 508 */ 509 updateAttribute: function (id, messages) { 510 var attribute = methods.find.call(this, id); 511 if (attribute != undefined) { 512 var msg = {}; 513 msg[id] = messages; 514 updateInput($(this), attribute, msg); 515 } 516 } 517 }; 518 519 var watchAttribute = function ($form, attribute) { 520 var $input = findInput($form, attribute); 521 if (attribute.validateOnChange) { 522 $input.on('change.yiiActiveForm', function () { 523 validateAttribute($form, attribute, false); 524 }); 525 } 526 if (attribute.validateOnBlur) { 527 $input.on('blur.yiiActiveForm', function () { 528 if (attribute.status == 0 || attribute.status == 1) { 529 validateAttribute($form, attribute, true); 530 } 531 }); 532 } 533 if (attribute.validateOnType) { 534 $input.on('keyup.yiiActiveForm', function (e) { 535 if ($.inArray(e.which, [16, 17, 18, 37, 38, 39, 40]) !== -1) { 536 return; 537 } 538 if (attribute.value !== getValue($form, attribute)) { 539 validateAttribute($form, attribute, false, attribute.validationDelay); 540 } 541 }); 542 } 543 }; 544 545 var unwatchAttribute = function ($form, attribute) { 546 findInput($form, attribute).off('.yiiActiveForm'); 547 }; 548 549 var validateAttribute = function ($form, attribute, forceValidate, validationDelay) { 550 var data = $form.data('yiiActiveForm'); 551 552 if (forceValidate) { 553 attribute.status = 2; 554 } 555 $.each(data.attributes, function () { 556 if (!isEqual(this.value, getValue($form, this))) { 557 this.status = 2; 558 forceValidate = true; 559 } 560 }); 561 if (!forceValidate) { 562 return; 563 } 564 565 if (data.settings.timer !== undefined) { 566 clearTimeout(data.settings.timer); 567 } 568 data.settings.timer = window.setTimeout(function () { 569 if (data.submitting || $form.is(':hidden')) { 570 return; 571 } 572 $.each(data.attributes, function () { 573 if (this.status === 2) { 574 this.status = 3; 575 576 var $container = $form.find(this.container), 577 $input = findInput($form, this); 578
579 var $errorElement = data.settings.validationStateOn === 'input' ? $input : $container; 580 581 $errorElement.addClass(data.settings.validatingCssClass); 582 } 583 }); 584 methods.validate.call($form); 585 }, validationDelay ? validationDelay : 200); 586 }; 587 588 /** 589 * Compares two value whatever it objects, arrays or simple types 590 * @param val1 591 * @param val2 592 * @returns boolean 593 */ 594 var isEqual = function (val1, val2) { 595 // objects 596 if (val1 instanceof Object) { 597 return isObjectsEqual(val1, val2) 598 } 599 600 // arrays 601 if (Array.isArray(val1)) { 602 return isArraysEqual(val1, val2); 603 } 604 605 // simple types 606 return val1 === val2; 607 }; 608 609 /** 610 * Compares two objects 611 * @param obj1 612 * @param obj2 613 * @returns boolean 614 */ 615 var isObjectsEqual = function (obj1, obj2) { 616 if (!(obj1 instanceof Object) || !(obj2 instanceof Object)) { 617 return false; 618 } 619 620 var keys1 = Object.keys(obj1); 621 var keys2 = Object.keys(obj2); 622 if (keys1.length !== keys2.length) { 623 return false; 624 } 625 626 for (var i = 0; i < keys1.length; i += 1) { 627 if (!obj2.hasOwnProperty(keys1[i])) { 628 return false; 629 } 630 if (obj1[keys1[i]] !== obj2[keys1[i]]) { 631 return false; 632 } 633 } 634 635 return true; 636 }; 637 638 /** 639 * Compares two arrays 640 * @param arr1 641 * @param arr2 642 * @returns boolean 643 */ 644 var isArraysEqual = function (arr1, arr2) { 645 if (!Array.isArray(arr1) || !Array.isArray(arr2)) { 646 return false; 647 } 648 649 if (arr1.length !== arr2.length) { 650 return false; 651 } 652 for (var i = 0; i < arr1.length; i += 1) { 653 if (arr1[i] !== arr2[i]) { 654 return false; 655 } 656 } 657 return true; 658 }; 659 660 /** 661 * Returns an array prototype with a shortcut method for adding a new deferred. 662 * The context of the callback will be the deferred object so it can be resolved like ```this.resolve()``` 663 * @returns Array 664 */ 665 var deferredArray = function () { 666 var array = []; 667 array.add = function (callback) { 668 this.push(new $.Deferred(callback)); 669 }; 670 return array; 671 }; 672 673 var buttonOptions = ['action', 'target', 'method', 'enctype']; 674 675 /** 676 * Returns current form options 677 * @param $form 678 * @returns object Object with button of form options 679 */ 680 var getFormOptions = function ($form) { 681 var attributes = {}; 682 for (var i = 0; i < buttonOptions.length; i++) { 683 attributes[buttonOptions[i]] = $form.attr(buttonOptions[i]); 684 } 685 686 return attributes; 687 }; 688 689 /** 690 * Applies temporary form options related to submit button 691 * @param $form the form jQuery object 692 * @param $button the button jQuery object 693 */ 694 var applyButtonOptions = function ($form, $button) { 695 for (var i = 0; i < buttonOptions.length; i++) { 696 var value = $button.attr('form' + buttonOptions[i]); 697 if (value) { 698 $form.attr(buttonOptions[i], value); 699 } 700 } 701 }; 702 703 /** 704 * Restores original form options 705 * @param $form the form jQuery object 706 */ 707 var restoreButtonOptions = function ($form) { 708 var data = $form.data('yiiActiveForm'); 709 710 for (var i = 0; i < buttonOptions.length; i++) { 711 $form.attr(buttonOptions[i], data.options[buttonOptions[i]] || null); 712 } 713 }; 714 715 /** 716 * Updates the error messages and the input containers for all applicable attributes 717 * @param $form the form jQuery object 718 * @param messages array the validation error messages 719 * @param submitting whether this method is called after validation triggered by form submission 720 */ 721 var updateInputs = function ($form, messages, submitting) { 722 var data = $form.data('yiiActiveForm'); 723 724 if (data === undefined) { 725 return false;
726 } 727 728 var errorAttributes = [], $input; 729 $.each(data.attributes, function () { 730 var hasError = (submitting && updateInput($form, this, messages)) || (!submitting && attrHasError($form, 731 this, messages)); 732 $input = findInput($form, this); 733 734 if (!$input.is(':disabled') && !this.cancelled && hasError) { 735 errorAttributes.push(this); 736 } 737 }); 738 739 $form.trigger(events.afterValidate, [messages, errorAttributes]); 740 741 if (submitting) { 742 updateSummary($form, messages); 743 if (errorAttributes.length) { 744 if (data.settings.scrollToError) { 745 var h = $(document).height(), top = $form.find($.map(errorAttributes, function (attribute) { 746 return attribute.input; 747 }).join(',')).first().closest(':visible').offset().top - data.settings.scrollToErrorOffset; 748 top = top < 0 ? 0 : (top > h ? h : top); 749 var wtop = $(window).scrollTop(); 750 if (top < wtop || top > wtop + $(window).height()) { 751 $(window).scrollTop(top); 752 } 753 } 754 data.submitting = false; 755 } else { 756 data.validated = true; 757 if (data.submitObject) { 758 applyButtonOptions($form, data.submitObject); 759 } 760 $form.submit(); 761 if (data.submitObject) { 762 restoreButtonOptions($form); 763 } 764 } 765 } else { 766 $.each(data.attributes, function () { 767 if (!this.cancelled && (this.status === 2 || this.status === 3)) { 768 updateInput($form, this, messages); 769 } 770 }); 771 } 772 submitFinalize($form); 773 }; 774 775 /** 776 * Updates hidden field that represents clicked submit button. 777 * @param $form the form jQuery object. 778 */ 779 var updateHiddenButton = function ($form) { 780 var data = $form.data('yiiActiveForm'); 781 var $button = data.submitObject || $form.find(':submit:first'); 782 // TODO: if the submission is caused by "change" event, it will not work 783 if ($button.length && $button.attr('type') == 'submit' && $button.attr('name')) { 784 // simulate button input value 785 var $hiddenButton = $('input[type="hidden"][name="' + $button.attr('name') + '"]', $form); 786 if (!$hiddenButton.length) { 787 $('<input>').attr({ 788 type: 'hidden', 789 name: $button.attr('name'), 790 value: $button.attr('value') 791 }).appendTo($form); 792 } else { 793 $hiddenButton.attr('value', $button.attr('value')); 794 } 795 } 796 }; 797 798 /** 799 * Updates the error message and the input container for a particular attribute. 800 * @param $form the form jQuery object 801 * @param attribute object the configuration for a particular attribute. 802 * @param messages array the validation error messages 803 * @return boolean whether there is a validation error for the specified attribute 804 */ 805 var updateInput = function ($form, attribute, messages) { 806 var data = $form.data('yiiActiveForm'), 807 $input = findInput($form, attribute), 808 hasError = attrHasError($form, attribute, messages); 809 810 if (!$.isArray(messages[attribute.id])) { 811 messages[attribute.id] = []; 812 } 813 814 attribute.status = 1; 815 if ($input.length) { 816 var $container = $form.find(attribute.container); 817 var $error = $container.find(attribute.error); 818 updateAriaInvalid($form, attribute, hasError); 819 820 var $errorElement = data.settings.validationStateOn === 'input' ? $input : $container; 821 822 if (hasError) { 823 if (attribute.encodeError) { 824 $error.text(messages[attribute.id][0]); 825 } else { 826 $error.html(messages[attribute.id][0]); 827 }
828 $errorElement.removeClass(data.settings.validatingCssClass + ' ' + data.settings.successCssClass) 829 .addClass(data.settings.errorCssClass); 830 } else { 831 $error.empty(); 832 $errorElement.removeClass(data.settings.validatingCssClass + ' ' + data.settings.errorCssClass + ' ') 833 .addClass(data.settings.successCssClass); 834 } 835 attribute.value = getValue($form, attribute); 836 } 837 838 $form.trigger(events.afterValidateAttribute, [attribute, messages[attribute.id]]); 839 840 return hasError; 841 }; 842 843 /** 844 * Checks if a particular attribute has an error 845 * @param $form the form jQuery object 846 * @param attribute object the configuration for a particular attribute. 847 * @param messages array the validation error messages 848 * @return boolean whether there is a validation error for the specified attribute 849 */ 850 var attrHasError = function ($form, attribute, messages) { 851 var $input = findInput($form, attribute), 852 hasError = false; 853 854 if (!$.isArray(messages[attribute.id])) { 855 messages[attribute.id] = []; 856 } 857 858 if ($input.length) { 859 hasError = messages[attribute.id].length > 0; 860 } 861 862 return hasError; 863 }; 864 865 /** 866 * Updates the error summary. 867 * @param $form the form jQuery object 868 * @param messages array the validation error messages 869 */ 870 var updateSummary = function ($form, messages) { 871 var data = $form.data('yiiActiveForm'), 872 $summary = $form.find(data.settings.errorSummary), 873 $ul = $summary.find('ul').empty(); 874 875 if ($summary.length && messages) { 876 $.each(data.attributes, function () { 877 if ($.isArray(messages[this.id]) && messages[this.id].length) { 878 var error = $('<li/>'); 879 if (data.settings.encodeErrorSummary) { 880 error.text(messages[this.id][0]); 881 } else { 882 error.html(messages[this.id][0]); 883 } 884 $ul.append(error); 885 } 886 }); 887 $summary.toggle($ul.find('li').length > 0); 888 } 889 }; 890 891 var getValue = function ($form, attribute) { 892 var $input = findInput($form, attribute); 893 var type = $input.attr('type'); 894 if (type === 'checkbox' || type === 'radio') { 895 var $realInput = $input.filter(':checked'); 896 if ($realInput.length > 1) { 897 var values = []; 898 $realInput.each(function (index) { 899 values.push($($realInput.get(index)).val()); 900 }); 901 return values; 902 } 903 904 if (!$realInput.length) { 905 $realInput = $form.find('input[type=hidden][name="' + $input.attr('name') + '"]'); 906 } 907 908 return $realInput.val(); 909 } else { 910 return $input.val(); 911 } 912 }; 913 914 var findInput = function ($form, attribute) { 915 var $input = $form.find(attribute.input); 916 if ($input.length && $input[0].tagName.toLowerCase() === 'div') { 917 // checkbox list or radio list 918 return $input.find('input'); 919 } else { 920 return $input; 921 } 922 }; 923 924 var updateAriaInvalid = function ($form, attribute, hasError) { 925 if (attribute.updateAriaInvalid) { 926 $form.find(attribute.input).attr('aria-invalid', hasError ? 'true' : 'false'); 927 } 928 } 929})(window.jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.