Skip to content

Instantly share code, notes, and snippets.

@bscheshirwork
Created January 30, 2020 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bscheshirwork/c5543a408eca448e5e920c85f79d458e to your computer and use it in GitHub Desktop.
Save bscheshirwork/c5543a408eca448e5e920c85f79d458e to your computer and use it in GitHub Desktop.
yii.activeForm and jquery onSubmit for this form. Do not call twice.
$('.feedback-form__form').on('submit', (function (e) {
// first time call before first call yii.activeForm submitForm
// set $.Deferred() in submitForm ()
// second time call before second call yii.activeForm submitForm
// note: fire submit again after ajax
// don't call form twice - js validate already is true
console.log('feedback-form__form submit');
let $form = $(this),
submitButton = $('input[type="submit"]', $form),
data = $form.data('yiiActiveForm'),
promiseReady = false,
sendAjaxRequest = function () {
console.log('sendAjaxRequest : true+false or $.when($form.data(\'yiiSubmitFinalizePromise\')).done');
promiseReady = false;
let url = $form.attr('action'),
formData = new FormData($form[0]),
feedbackFormSuccess = function (data) {
console.log('feedbackFormSuccess');
if (data.success) {
console.log('data.success');
$('.feedback__result', $form.parent('.modal-body')).text(data.success).addClass('feedback__result--success').removeClass('hide');
$form.yiiActiveForm('resetForm');
$form[0].reset();
} else {
console.log('!data.success');
$form.yiiActiveForm('updateMessages', data.messages);
}
data.validated = false;
console.log('data.validated ' + data.validated);
submitButton.attr('disabled', false);
},
feedbackFormError = function (data) {
console.log('feedbackFormError');
let responseJSON = data.responseJSON;
if (responseJSON.message) {
$('.feedback__result', $form.parent('.modal-body')).text(responseJSON.message).addClass('feedback__result--error').removeClass('hide');
}
submitButton.attr('disabled', false);
};
$.ajax({
type: 'POST',
url: url,
data: formData,
cache: false,
contentType: false,
processData: false,
success: feedbackFormSuccess,
error: feedbackFormError
});
}
;
console.log('data.validated ' + data.validated);
console.log('data.submitting ' + data.submitting);
console.log('$form.data(yiiSubmitFinalizePromise) ' + $form.data('yiiSubmitFinalizePromise'));
if (data.validated && !data.submitting) {
// no not call twice in this case
sendAjaxRequest();
} else {
if ($form.data('yiiSubmitFinalizePromise') && !promiseReady) {
promiseReady = true;
$.when($form.data('yiiSubmitFinalizePromise')).done(sendAjaxRequest);
}
}
// cleaning previous send result
$('.feedback__result', $form.parent('.modal-body')).text('').addClass('hide').removeClass('feedback__result--error').removeClass('feedback__result--success');
e.preventDefault();
// disable submit
submitButton.attr('disabled', true);
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment