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