Skip to content

Commit

Permalink
Fixes yiisoft#8161: Fixed active form data-method submit bug when c…
Browse files Browse the repository at this point in the history
…lient validation is used
  • Loading branch information
vbelogai authored and samdark committed Jul 27, 2015
1 parent b16d734 commit 1d6a340
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Yii Framework 2 Change Log
- Bug #7374: Use proper INSERT syntax with default values when no values are specified (nineinchnick)
- Bug #7707: client-side `trim` validator now passes the trimmed value to subsequent validators (nkovacs)
- Bug #7764: `\yii\helpers\ArrayHelper::toArray()` wasn't passing `$recursive` to `Arrayable::toArray` (brandonkelly)
- Bug #8161: Fixed active form `data-method` submit bug when client validation is used (vbelogai)
- Bug #8322: `yii\behaviors\TimestampBehavior::touch()` now throws an exception if owner is new record (klimov-paul)
- Bug #8451: `yii\i18n\Formatter` did not allow negative unix timestamps as input for date formatting (cebe)
- Bug #8483: sequence name in `Schema::getLastInsertId()` was not properly quoted (nineinchnick)
Expand Down
26 changes: 26 additions & 0 deletions framework/assets/yii.activeForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@
value: undefined
};


var submitDefer;

var setSubmitFinalizeDefer = function($form) {
submitDefer = $.Deferred();
$form.data('yiiSubmitFinalizePromise', submitDefer.promise());
};

// finalize yii.js $form.submit
var submitFinalize = function($form) {
if(submitDefer) {
submitDefer.resolve();
submitDefer = undefined;
$form.removeData('yiiSubmitFinalizePromise');
}
};


var methods = {
init: function (attributes, options) {
return this.each(function () {
Expand Down Expand Up @@ -273,6 +291,7 @@
$form.trigger(event, [messages, deferreds]);
if (event.result === false) {
data.submitting = false;
submitFinalize($form);
return;
}
}
Expand Down Expand Up @@ -341,6 +360,7 @@
},
error: function () {
data.submitting = false;
submitFinalize($form);
}
});
} else if (data.submitting) {
Expand All @@ -359,15 +379,20 @@
data = $form.data('yiiActiveForm');

if (data.validated) {
// Second submit's call (from validate/updateInputs)
data.submitting = false;
var event = $.Event(events.beforeSubmit);
$form.trigger(event);
if (event.result === false) {
data.validated = false;
submitFinalize($form);
return false;
}
return true; // continue submitting the form since validation passes
} else {
// First submit's call (from yii.js/handleAction) - execute validating
setSubmitFinalizeDefer($form);

if (data.settings.timer !== undefined) {
clearTimeout(data.settings.timer);
}
Expand Down Expand Up @@ -531,6 +556,7 @@
}
});
}
submitFinalize($form);
};

/**
Expand Down
31 changes: 17 additions & 14 deletions framework/assets/yii.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,25 @@ yii = (function ($) {
}

$form.trigger('submit');
$.when($form.data('yiiSubmitFinalizePromise')).then(
function () {
if (oldAction != null) {
$form.attr('action', oldAction);
}
$form.attr('method', oldMethod);

if (oldAction != null) {
$form.attr('action', oldAction);
}
$form.attr('method', oldMethod);

// remove the temporarily added hidden inputs
if (params && $.isPlainObject(params)) {
$.each(params, function (idx, obj) {
$('input[name="' + idx + '"]', $form).remove();
});
}
// remove the temporarily added hidden inputs
if (params && $.isPlainObject(params)) {
$.each(params, function (idx, obj) {
$('input[name="' + idx + '"]', $form).remove();
});
}

if (newForm) {
$form.remove();
}
if (newForm) {
$form.remove();
}
}
);
},

getQueryParams: function (url) {
Expand Down

0 comments on commit 1d6a340

Please sign in to comment.