Commit 51863437 by Alex-Code

deferred validation

Added support of deferred to ```ActiveForm``` validation.
parent 1e9350ab
...@@ -280,24 +280,25 @@ ...@@ -280,24 +280,25 @@
var validate = function ($form, successCallback, errorCallback) { var validate = function ($form, successCallback, errorCallback) {
var data = $form.data('yiiActiveForm'), var data = $form.data('yiiActiveForm'),
needAjaxValidation = false, needAjaxValidation = false,
messages = {}; messages = {},
deferreds = [];
$.each(data.attributes, function () { $.each(data.attributes, function () {
if (data.submitting || this.status === 2 || this.status === 3) { if (data.submitting || this.status === 2 || this.status === 3) {
var msg = []; var msg = [];
messages[this.id] = msg;
if (!data.settings.beforeValidate || data.settings.beforeValidate($form, this, msg)) { if (!data.settings.beforeValidate || data.settings.beforeValidate($form, this, msg)) {
if (this.validate) { if (this.validate) {
this.validate(this, getValue($form, this), msg); this.validate(this, getValue($form, this), msg, deferreds);
} }
if (msg.length) { if (this.enableAjaxValidation) {
messages[this.id] = msg;
} else if (this.enableAjaxValidation) {
needAjaxValidation = true; needAjaxValidation = true;
} }
} }
} }
}); });
$.when.apply(this, deferreds).always(function() {
if (needAjaxValidation && (!data.submitting || $.isEmptyObject(messages))) { if (needAjaxValidation && (!data.submitting || $.isEmptyObject(messages))) {
// Perform ajax validation when at least one input needs it. // Perform ajax validation when at least one input needs it.
// If the validation is triggered by form submission, ajax validation // If the validation is triggered by form submission, ajax validation
...@@ -344,6 +345,7 @@ ...@@ -344,6 +345,7 @@
} else { } else {
successCallback(messages); successCallback(messages);
} }
});
}; };
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment