|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769 |
-
- (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);
|