|
-
- (function ($) {
-
- $.fn.yiiActiveForm = function (method) {
- if (methods[method]) {
- return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
- } else if (typeof method === 'object' || !method) {
- return methods.init.apply(this, arguments);
- } else {
- $.error('Method ' + method + ' does not exist on jQuery.yiiActiveForm');
- return false;
- }
- };
-
- var events = {
-
-
- beforeValidate: 'beforeValidate',
-
-
- afterValidate: 'afterValidate',
-
-
- beforeValidateAttribute: 'beforeValidateAttribute',
-
-
- afterValidateAttribute: 'afterValidateAttribute',
-
-
- beforeSubmit: 'beforeSubmit',
-
-
- ajaxBeforeSend: 'ajaxBeforeSend',
-
-
- ajaxComplete: 'ajaxComplete',
-
-
- afterInit: 'afterInit'
- };
-
-
- var defaults = {
-
- encodeErrorSummary: true,
-
- errorSummary: '.error-summary',
-
- validateOnSubmit: true,
-
- errorCssClass: 'has-error',
-
- successCssClass: 'has-success',
-
- validatingCssClass: 'validating',
-
- ajaxParam: 'ajax',
-
- ajaxDataType: 'json',
-
- validationUrl: undefined,
-
- scrollToError: true
- };
-
-
- var attributeDefaults = {
-
- id: undefined,
-
- name: undefined,
-
- container: undefined,
-
- input: undefined,
-
- error: '.help-block',
-
- encodeError: true,
-
- validateOnChange: true,
-
- validateOnBlur: true,
-
- validateOnType: false,
-
- validationDelay: 500,
-
- enableAjaxValidation: false,
-
- validate: undefined,
-
- status: 0,
-
- cancelled: false,
-
- value: undefined
- };
-
-
- var submitDefer;
-
- var setSubmitFinalizeDefer = function($form) {
- submitDefer = $.Deferred();
- $form.data('yiiSubmitFinalizePromise', submitDefer.promise());
- };
-
-
- var submitFinalize = function($form) {
- if(submitDefer) {
- submitDefer.resolve();
- submitDefer = undefined;
- $form.removeData('yiiSubmitFinalizePromise');
- }
- };
-
-
- var methods = {
- init: function (attributes, options) {
- return this.each(function () {
- var $form = $(this);
- if ($form.data('yiiActiveForm')) {
- return;
- }
-
- var settings = $.extend({}, defaults, options || {});
- if (settings.validationUrl === undefined) {
- settings.validationUrl = $form.attr('action');
- }
-
- $.each(attributes, function (i) {
- attributes[i] = $.extend({value: getValue($form, this)}, attributeDefaults, this);
- watchAttribute($form, attributes[i]);
- });
-
- $form.data('yiiActiveForm', {
- settings: settings,
- attributes: attributes,
- submitting: false,
- validated: false,
- options: getFormOptions($form)
- });
-
-
-
- $form.bind('reset.yiiActiveForm', methods.resetForm);
-
- if (settings.validateOnSubmit) {
- $form.on('mouseup.yiiActiveForm keyup.yiiActiveForm', ':submit', function () {
- $form.data('yiiActiveForm').submitObject = $(this);
- });
- $form.on('submit.yiiActiveForm', methods.submitForm);
- }
- var event = $.Event(events.afterInit);
- $form.trigger(event);
- });
- },
-
-
-
- add: function (attribute) {
- var $form = $(this);
- attribute = $.extend({value: getValue($form, attribute)}, attributeDefaults, attribute);
- $form.data('yiiActiveForm').attributes.push(attribute);
- watchAttribute($form, attribute);
- },
-
-
- remove: function (id) {
- var $form = $(this),
- attributes = $form.data('yiiActiveForm').attributes,
- index = -1,
- attribute = undefined;
- $.each(attributes, function (i) {
- if (attributes[i]['id'] == id) {
- index = i;
- attribute = attributes[i];
- return false;
- }
- });
- if (index >= 0) {
- attributes.splice(index, 1);
- unwatchAttribute($form, attribute);
- }
- return attribute;
- },
-
-
- validateAttribute: function (id) {
- var attribute = methods.find.call(this, id);
- if (attribute != undefined) {
- validateAttribute($(this), attribute, true);
- }
- },
-
-
- find: function (id) {
- var attributes = $(this).data('yiiActiveForm').attributes,
- result = undefined;
- $.each(attributes, function (i) {
- if (attributes[i]['id'] == id) {
- result = attributes[i];
- return false;
- }
- });
- return result;
- },
-
- destroy: function () {
- return this.each(function () {
- $(this).unbind('.yiiActiveForm');
- $(this).removeData('yiiActiveForm');
- });
- },
-
- data: function () {
- return this.data('yiiActiveForm');
- },
-
-
- validate: function (forceValidate) {
- if (forceValidate) {
- $(this).data('yiiActiveForm').submitting = true;
- }
-
- var $form = $(this),
- data = $form.data('yiiActiveForm'),
- needAjaxValidation = false,
- messages = {},
- deferreds = deferredArray(),
- submitting = data.submitting;
-
- if (submitting) {
- var event = $.Event(events.beforeValidate);
- $form.trigger(event, [messages, deferreds]);
-
- if (event.result === false) {
- data.submitting = false;
- submitFinalize($form);
- return;
- }
- }
-
-
- $.each(data.attributes, function () {
- this.$form = $form;
- if (!$(this.input).is(":disabled")) {
- this.cancelled = false;
-
- if (data.submitting || this.status === 2 || this.status === 3) {
- var msg = messages[this.id];
- if (msg === undefined) {
- msg = [];
- messages[this.id] = msg;
- }
- var event = $.Event(events.beforeValidateAttribute);
- $form.trigger(event, [this, msg, deferreds]);
- if (event.result !== false) {
- if (this.validate) {
- this.validate(this, getValue($form, this), msg, deferreds, $form);
- }
- if (this.enableAjaxValidation) {
- needAjaxValidation = true;
- }
- } else {
- this.cancelled = true;
- }
- }
- }
- });
-
-
- $.when.apply(this, deferreds).always(function() {
-
- for (var i in messages) {
- if (0 === messages[i].length) {
- delete messages[i];
- }
- }
- if (needAjaxValidation && ($.isEmptyObject(messages) || data.submitting)) {
- var $button = data.submitObject,
- extData = '&' + data.settings.ajaxParam + '=' + $form.attr('id');
- if ($button && $button.length && $button.attr('name')) {
- extData += '&' + $button.attr('name') + '=' + $button.attr('value');
- }
- $.ajax({
- url: data.settings.validationUrl,
- type: $form.attr('method'),
- data: $form.serialize() + extData,
- dataType: data.settings.ajaxDataType,
- complete: function (jqXHR, textStatus) {
- $form.trigger(events.ajaxComplete, [jqXHR, textStatus]);
- },
- beforeSend: function (jqXHR, settings) {
- $form.trigger(events.ajaxBeforeSend, [jqXHR, settings]);
- },
- success: function (msgs) {
- if (msgs !== null && typeof msgs === 'object') {
- $.each(data.attributes, function () {
- if (!this.enableAjaxValidation || this.cancelled) {
- delete msgs[this.id];
- }
- });
- updateInputs($form, $.extend(messages, msgs), submitting);
- } else {
- updateInputs($form, messages, submitting);
- }
- },
- error: function () {
- data.submitting = false;
- submitFinalize($form);
- }
- });
- } else if (data.submitting) {
-
- setTimeout(function () {
- updateInputs($form, messages, submitting);
- }, 200);
- } else {
- updateInputs($form, messages, submitting);
- }
- });
- },
-
- submitForm: function () {
- var $form = $(this),
- data = $form.data('yiiActiveForm');
-
- if (data.validated) {
-
- data.submitting = false;
- var event = $.Event(events.beforeSubmit);
- $form.trigger(event);
- if (event.result === false) {
- data.validated = false;
- submitFinalize($form);
- return false;
- }
- updateHiddenButton($form);
- return true;
- } else {
-
- setSubmitFinalizeDefer($form);
-
- if (data.settings.timer !== undefined) {
- clearTimeout(data.settings.timer);
- }
- data.submitting = true;
- methods.validate.call($form);
- return false;
- }
- },
-
- resetForm: function () {
- var $form = $(this);
- var data = $form.data('yiiActiveForm');
-
-
-
- setTimeout(function () {
- $.each(data.attributes, function () {
-
- this.value = getValue($form, this);
- this.status = 0;
- var $container = $form.find(this.container);
- $container.removeClass(
- data.settings.validatingCssClass + ' ' +
- data.settings.errorCssClass + ' ' +
- data.settings.successCssClass
- );
- $container.find(this.error).html('');
- });
- $form.find(data.settings.errorSummary).hide().find('ul').html('');
- }, 1);
- },
-
-
-
- updateMessages: function (messages, summary) {
- var $form = $(this);
- var data = $form.data('yiiActiveForm');
- $.each(data.attributes, function () {
- updateInput($form, this, messages);
- });
- if (summary) {
- updateSummary($form, messages);
- }
- },
-
-
-
- updateAttribute: function(id, messages) {
- var attribute = methods.find.call(this, id);
- if (attribute != undefined) {
- var msg = {};
- msg[id] = messages;
- updateInput($(this), attribute, msg);
- }
- }
-
- };
-
- var watchAttribute = function ($form, attribute) {
- var $input = findInput($form, attribute);
- if (attribute.validateOnChange) {
- $input.on('change.yiiActiveForm', function () {
- validateAttribute($form, attribute, false);
- });
- }
- if (attribute.validateOnBlur) {
- $input.on('blur.yiiActiveForm', function () {
- if (attribute.status == 0 || attribute.status == 1) {
- validateAttribute($form, attribute, true);
- }
- });
- }
- if (attribute.validateOnType) {
- $input.on('keyup.yiiActiveForm', function (e) {
- if ($.inArray(e.which, [16, 17, 18, 37, 38, 39, 40]) !== -1 ) {
- return;
- }
- if (attribute.value !== getValue($form, attribute)) {
- validateAttribute($form, attribute, false, attribute.validationDelay);
- }
- });
- }
- };
-
- var unwatchAttribute = function ($form, attribute) {
- findInput($form, attribute).off('.yiiActiveForm');
- };
-
- var validateAttribute = function ($form, attribute, forceValidate, validationDelay) {
- var data = $form.data('yiiActiveForm');
-
- if (forceValidate) {
- attribute.status = 2;
- }
- $.each(data.attributes, function () {
- if (this.value !== getValue($form, this)) {
- this.status = 2;
- forceValidate = true;
- }
- });
- if (!forceValidate) {
- return;
- }
-
- if (data.settings.timer !== undefined) {
- clearTimeout(data.settings.timer);
- }
- data.settings.timer = setTimeout(function () {
- if (data.submitting || $form.is(':hidden')) {
- return;
- }
- $.each(data.attributes, function () {
- if (this.status === 2) {
- this.status = 3;
- $form.find(this.container).addClass(data.settings.validatingCssClass);
- }
- });
- methods.validate.call($form);
- }, validationDelay ? validationDelay : 200);
- };
-
-
-
- var deferredArray = function () {
- var array = [];
- array.add = function(callback) {
- this.push(new $.Deferred(callback));
- };
- return array;
- };
-
- var buttonOptions = ['action', 'target', 'method', 'enctype'];
-
-
-
- var getFormOptions = function ($form) {
- var attributes = {};
- for (var i = 0; i < buttonOptions.length; i++) {
- attributes[buttonOptions[i]] = $form.attr(buttonOptions[i]);
- }
- return attributes;
- };
-
-
-
- var applyButtonOptions = function ($form, $button) {
- for (var i = 0; i < buttonOptions.length; i++) {
- var value = $button.attr('form' + buttonOptions[i]);
- if (value) {
- $form.attr(buttonOptions[i], value);
- }
- }
- };
-
-
-
- var restoreButtonOptions = function ($form) {
- var data = $form.data('yiiActiveForm');
-
- for (var i = 0; i < buttonOptions.length; i++) {
- $form.attr(buttonOptions[i], data.options[buttonOptions[i]] || null);
- }
- };
-
-
-
- var updateInputs = function ($form, messages, submitting) {
- var data = $form.data('yiiActiveForm');
-
- if (data === undefined) {
- return false;
- }
-
- if (submitting) {
- var errorAttributes = [];
- $.each(data.attributes, function () {
- if (!$(this.input).is(":disabled") && !this.cancelled && updateInput($form, this, messages)) {
- errorAttributes.push(this);
- }
- });
-
- $form.trigger(events.afterValidate, [messages, errorAttributes]);
-
- updateSummary($form, messages);
-
- if (errorAttributes.length) {
- if (data.settings.scrollToError) {
- var top = $form.find($.map(errorAttributes, function(attribute) {
- return attribute.input;
- }).join(',')).first().closest(':visible').offset().top;
- var wtop = $(window).scrollTop();
- if (top < wtop || top > wtop + $(window).height()) {
- $(window).scrollTop(top);
- }
- }
- data.submitting = false;
- } else {
- data.validated = true;
- if (data.submitObject) {
- data.submitObject.trigger("click");
- } else {
- $form.submit();
- }
- }
- } else {
- $.each(data.attributes, function () {
- if (!this.cancelled && (this.status === 2 || this.status === 3)) {
- updateInput($form, this, messages);
- }
- });
- }
- submitFinalize($form);
- };
-
-
-
- var updateHiddenButton = function ($form) {
- var data = $form.data('yiiActiveForm');
- var $button = data.submitObject || $form.find(':submit:first');
-
- if ($button.length && $button.attr('type') == 'submit' && $button.attr('name')) {
-
- var $hiddenButton = $('input[type="hidden"][name="' + $button.attr('name') + '"]', $form);
- if (!$hiddenButton.length) {
- $('<input>').attr({
- type: 'hidden',
- name: $button.attr('name'),
- value: $button.attr('value')
- }).appendTo($form);
- } else {
- $hiddenButton.attr('value', $button.attr('value'));
- }
- }
- };
-
-
-
- var updateInput = function ($form, attribute, messages) {
- var data = $form.data('yiiActiveForm'),
- $input = findInput($form, attribute),
- hasError = false;
-
- if (!$.isArray(messages[attribute.id])) {
- messages[attribute.id] = [];
- }
- $form.trigger(events.afterValidateAttribute, [attribute, messages[attribute.id]]);
-
- attribute.status = 1;
- if ($input.length) {
- hasError = messages[attribute.id].length > 0;
- var $container = $form.find(attribute.container);
- var $error = $container.find(attribute.error);
- if (hasError) {
- if (attribute.encodeError) {
- $error.text(messages[attribute.id][0]);
- } else {
- $error.html(messages[attribute.id][0]);
- }
- $container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.successCssClass)
- .addClass(data.settings.errorCssClass);
- } else {
- $error.empty();
- $container.removeClass(data.settings.validatingCssClass + ' ' + data.settings.errorCssClass + ' ')
- .addClass(data.settings.successCssClass);
- }
- attribute.value = getValue($form, attribute);
- }
- return hasError;
- };
-
-
-
- var updateSummary = function ($form, messages) {
- var data = $form.data('yiiActiveForm'),
- $summary = $form.find(data.settings.errorSummary),
- $ul = $summary.find('ul').empty();
-
- if ($summary.length && messages) {
- $.each(data.attributes, function () {
- if ($.isArray(messages[this.id]) && messages[this.id].length) {
- var error = $('<li/>');
- if (data.settings.encodeErrorSummary) {
- error.text(messages[this.id][0]);
- } else {
- error.html(messages[this.id][0]);
- }
- $ul.append(error);
- }
- });
- $summary.toggle($ul.find('li').length > 0);
- }
- };
-
- var getValue = function ($form, attribute) {
- var $input = findInput($form, attribute);
- var type = $input.attr('type');
- if (type === 'checkbox' || type === 'radio') {
- var $realInput = $input.filter(':checked');
- if (!$realInput.length) {
- $realInput = $form.find('input[type=hidden][name="' + $input.attr('name') + '"]');
- }
- return $realInput.val();
- } else {
- return $input.val();
- }
- };
-
- var findInput = function ($form, attribute) {
- var $input = $form.find(attribute.input);
- if ($input.length && $input[0].tagName.toLowerCase() === 'div') {
-
- return $input.find('input');
- } else {
- return $input;
- }
- };
-
- })(window.jQuery);
|