From 9fc4c3807b86df641fb6c16a4b39f5e450fa6159 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 3 May 2013 08:52:41 -0400 Subject: [PATCH] form wip --- framework/assets/yii.activeForm.js | 49 ++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/framework/assets/yii.activeForm.js b/framework/assets/yii.activeForm.js index 8de93c8..a835be2 100644 --- a/framework/assets/yii.activeForm.js +++ b/framework/assets/yii.activeForm.js @@ -123,7 +123,7 @@ }, submitForm: function () { - var $form = $(this), + var $form = this, data = $form.data('yiiActiveForm'); if (data.validated) { // continue submitting the form since validation passes @@ -142,7 +142,7 @@ hasError = updateInput($form, this, messages) || hasError; }); updateSummary($form, messages); - if (!data.settings.afterValidate || data.settings.afterValidate($form, data, hasError)) { + if (!data.settings.afterValidate || data.settings.afterValidate($form, messages, hasError)) { if (!hasError) { data.validated = true; var $button = data.submitObject || $form.find(':submit:first'); @@ -167,7 +167,7 @@ }, resetForm: function () { - var $form = $(this); + var $form = this; var data = $form.data('yiiActiveForm'); // Because we bind directly to a form reset event instead of a reset button (that may not exist), // when this function is executed form input values have not been reset yet. @@ -190,26 +190,6 @@ } }; - var getValue = function ($form, attribute) { - var $input = findInput($form, attribute); - var type = $input.attr('type'); - if (type === 'checkbox' || type === 'radio') { - return $input.filter(':checked').val(); - } else { - return $input.val(); - } - }; - - var findInput = function ($form, attribute) { - var $input = $form.find(attribute.input); - if ($input.length && $input[0].tagName.toLowerCase() === 'div') { - // checkbox list or radio list - return $input.find('input'); - } else { - return $input; - } - }; - var watchAttributes = function ($form, attributes) { $.each(attributes, function (i, attribute) { var $input = findInput($form, attribute); @@ -304,6 +284,9 @@ }); if (needAjaxValidation && (!data.submitting || $.isEmptyObject(messages))) { + // Perform ajax validation when at least one input needs it. + // If the validation is triggered by form submission, ajax validation + // should be done only when all inputs pass client validation var $button = data.submitObject, extData = '&' + data.settings.ajaxVar + '=' + $form.attr('id'); if ($button && $button.length && $button.attr('name')) { @@ -389,4 +372,24 @@ } }; + var getValue = function ($form, attribute) { + var $input = findInput($form, attribute); + var type = $input.attr('type'); + if (type === 'checkbox' || type === 'radio') { + return $input.filter(':checked').val(); + } else { + return $input.val(); + } + }; + + var findInput = function ($form, attribute) { + var $input = $form.find(attribute.input); + if ($input.length && $input[0].tagName.toLowerCase() === 'div') { + // checkbox list or radio list + return $input.find('input'); + } else { + return $input; + } + }; + })(window.jQuery); \ No newline at end of file