How to prevent form from submitting with AJAX validation
How to prevent form from submit when I am using ajax to validate it. The problem is that the ajax is asynchronous and the real result ( which is false
in my case ) comming later. Is making Ajax
to sync
is the right way? This is my piece of code:
$('#w0').on('beforeSubmit', function(e){
let loadDate = $('#dstrequest-load_date').val()
let shippingDate = $('#dstrequest-shipping_date').val()
let requestId = '".($model->isNewRecord ? 0 : $model->id)."'
let trucks = $('.container-items_truck').find('.truck-item')
let ids =
let result = true
$.each(trucks, function(){
let id = $(this).find('select').first().val()
if(ids.indexOf(id) === -1)
ids.push(id)
})
$.ajax({
method: 'post',
url: '/erp/distribution/dst-vehicle/check-truck-engage-date',
data: {
load_date: loadDate,
shipping_date: shippingDate,
trucks: ids,
request_id: requestId
}
})
.done(function(data){
let selects = $('select[name$="[truck_id]"]')
$.each(selects, function(){
let val = $(this).val()
if(data.indexOf(val) !== -1){
$('#w0').yiiActiveForm('updateAttribute', $(this).attr('id'), ['".Yii::t('distribution', 'distributionCore.busy_truck')."'])
result = false
}
})
})
.always(function(data){
let transfer_wrapper = $('.transfer-wrapper')
$.each(transfer_wrapper, function(){
if($(this).is(':visible')){
let fields = $(this).find('input, select')
$.each(fields, function(){
if($(this).val() == ''){
let id = $(this).attr('id')
$('#w0').yiiActiveForm('updateAttribute', id, ['" . Yii::t('distribution', 'distributionCore.please_fill_the_field') . "'])
result = false
}
})
}
})
})
// HERE THE RESULT SHOULD BE FALSE BECAUSE OF THE AJAX VALIDATION BUT IT'S TRUE BECAUSE OF THE ASYNC BEHAVIOUR.
return result
})
javascript ajax
add a comment |
How to prevent form from submit when I am using ajax to validate it. The problem is that the ajax is asynchronous and the real result ( which is false
in my case ) comming later. Is making Ajax
to sync
is the right way? This is my piece of code:
$('#w0').on('beforeSubmit', function(e){
let loadDate = $('#dstrequest-load_date').val()
let shippingDate = $('#dstrequest-shipping_date').val()
let requestId = '".($model->isNewRecord ? 0 : $model->id)."'
let trucks = $('.container-items_truck').find('.truck-item')
let ids =
let result = true
$.each(trucks, function(){
let id = $(this).find('select').first().val()
if(ids.indexOf(id) === -1)
ids.push(id)
})
$.ajax({
method: 'post',
url: '/erp/distribution/dst-vehicle/check-truck-engage-date',
data: {
load_date: loadDate,
shipping_date: shippingDate,
trucks: ids,
request_id: requestId
}
})
.done(function(data){
let selects = $('select[name$="[truck_id]"]')
$.each(selects, function(){
let val = $(this).val()
if(data.indexOf(val) !== -1){
$('#w0').yiiActiveForm('updateAttribute', $(this).attr('id'), ['".Yii::t('distribution', 'distributionCore.busy_truck')."'])
result = false
}
})
})
.always(function(data){
let transfer_wrapper = $('.transfer-wrapper')
$.each(transfer_wrapper, function(){
if($(this).is(':visible')){
let fields = $(this).find('input, select')
$.each(fields, function(){
if($(this).val() == ''){
let id = $(this).attr('id')
$('#w0').yiiActiveForm('updateAttribute', id, ['" . Yii::t('distribution', 'distributionCore.please_fill_the_field') . "'])
result = false
}
})
}
})
})
// HERE THE RESULT SHOULD BE FALSE BECAUSE OF THE AJAX VALIDATION BUT IT'S TRUE BECAUSE OF THE ASYNC BEHAVIOUR.
return result
})
javascript ajax
add a comment |
How to prevent form from submit when I am using ajax to validate it. The problem is that the ajax is asynchronous and the real result ( which is false
in my case ) comming later. Is making Ajax
to sync
is the right way? This is my piece of code:
$('#w0').on('beforeSubmit', function(e){
let loadDate = $('#dstrequest-load_date').val()
let shippingDate = $('#dstrequest-shipping_date').val()
let requestId = '".($model->isNewRecord ? 0 : $model->id)."'
let trucks = $('.container-items_truck').find('.truck-item')
let ids =
let result = true
$.each(trucks, function(){
let id = $(this).find('select').first().val()
if(ids.indexOf(id) === -1)
ids.push(id)
})
$.ajax({
method: 'post',
url: '/erp/distribution/dst-vehicle/check-truck-engage-date',
data: {
load_date: loadDate,
shipping_date: shippingDate,
trucks: ids,
request_id: requestId
}
})
.done(function(data){
let selects = $('select[name$="[truck_id]"]')
$.each(selects, function(){
let val = $(this).val()
if(data.indexOf(val) !== -1){
$('#w0').yiiActiveForm('updateAttribute', $(this).attr('id'), ['".Yii::t('distribution', 'distributionCore.busy_truck')."'])
result = false
}
})
})
.always(function(data){
let transfer_wrapper = $('.transfer-wrapper')
$.each(transfer_wrapper, function(){
if($(this).is(':visible')){
let fields = $(this).find('input, select')
$.each(fields, function(){
if($(this).val() == ''){
let id = $(this).attr('id')
$('#w0').yiiActiveForm('updateAttribute', id, ['" . Yii::t('distribution', 'distributionCore.please_fill_the_field') . "'])
result = false
}
})
}
})
})
// HERE THE RESULT SHOULD BE FALSE BECAUSE OF THE AJAX VALIDATION BUT IT'S TRUE BECAUSE OF THE ASYNC BEHAVIOUR.
return result
})
javascript ajax
How to prevent form from submit when I am using ajax to validate it. The problem is that the ajax is asynchronous and the real result ( which is false
in my case ) comming later. Is making Ajax
to sync
is the right way? This is my piece of code:
$('#w0').on('beforeSubmit', function(e){
let loadDate = $('#dstrequest-load_date').val()
let shippingDate = $('#dstrequest-shipping_date').val()
let requestId = '".($model->isNewRecord ? 0 : $model->id)."'
let trucks = $('.container-items_truck').find('.truck-item')
let ids =
let result = true
$.each(trucks, function(){
let id = $(this).find('select').first().val()
if(ids.indexOf(id) === -1)
ids.push(id)
})
$.ajax({
method: 'post',
url: '/erp/distribution/dst-vehicle/check-truck-engage-date',
data: {
load_date: loadDate,
shipping_date: shippingDate,
trucks: ids,
request_id: requestId
}
})
.done(function(data){
let selects = $('select[name$="[truck_id]"]')
$.each(selects, function(){
let val = $(this).val()
if(data.indexOf(val) !== -1){
$('#w0').yiiActiveForm('updateAttribute', $(this).attr('id'), ['".Yii::t('distribution', 'distributionCore.busy_truck')."'])
result = false
}
})
})
.always(function(data){
let transfer_wrapper = $('.transfer-wrapper')
$.each(transfer_wrapper, function(){
if($(this).is(':visible')){
let fields = $(this).find('input, select')
$.each(fields, function(){
if($(this).val() == ''){
let id = $(this).attr('id')
$('#w0').yiiActiveForm('updateAttribute', id, ['" . Yii::t('distribution', 'distributionCore.please_fill_the_field') . "'])
result = false
}
})
}
})
})
// HERE THE RESULT SHOULD BE FALSE BECAUSE OF THE AJAX VALIDATION BUT IT'S TRUE BECAUSE OF THE ASYNC BEHAVIOUR.
return result
})
javascript ajax
javascript ajax
asked Jan 2 at 11:58
Toma TomovToma Tomov
622417
622417
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Is making Ajax to sync is the right way?
No. Synchronous HTTP requests from JS are deprecated. Never use them.
You need to either:
Always halt
Always prevent normal form submission.
After you have validated the input, using Ajax, use JS to resubmit the form (without repeating validation).
Validate in advance
Do the validation as the data is entered, field by field.
Hopefully, the Ajax will be finished by the time the last field is completed and the form submitted.
That at that point, if the Ajax isn't finished, you either have to risk submitting the form without knowing the result of the Ajax validation or fall back to the first option I suggested.
Thank you very much!
– Toma Tomov
Jan 2 at 12:03
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005950%2fhow-to-prevent-form-from-submitting-with-ajax-validation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is making Ajax to sync is the right way?
No. Synchronous HTTP requests from JS are deprecated. Never use them.
You need to either:
Always halt
Always prevent normal form submission.
After you have validated the input, using Ajax, use JS to resubmit the form (without repeating validation).
Validate in advance
Do the validation as the data is entered, field by field.
Hopefully, the Ajax will be finished by the time the last field is completed and the form submitted.
That at that point, if the Ajax isn't finished, you either have to risk submitting the form without knowing the result of the Ajax validation or fall back to the first option I suggested.
Thank you very much!
– Toma Tomov
Jan 2 at 12:03
add a comment |
Is making Ajax to sync is the right way?
No. Synchronous HTTP requests from JS are deprecated. Never use them.
You need to either:
Always halt
Always prevent normal form submission.
After you have validated the input, using Ajax, use JS to resubmit the form (without repeating validation).
Validate in advance
Do the validation as the data is entered, field by field.
Hopefully, the Ajax will be finished by the time the last field is completed and the form submitted.
That at that point, if the Ajax isn't finished, you either have to risk submitting the form without knowing the result of the Ajax validation or fall back to the first option I suggested.
Thank you very much!
– Toma Tomov
Jan 2 at 12:03
add a comment |
Is making Ajax to sync is the right way?
No. Synchronous HTTP requests from JS are deprecated. Never use them.
You need to either:
Always halt
Always prevent normal form submission.
After you have validated the input, using Ajax, use JS to resubmit the form (without repeating validation).
Validate in advance
Do the validation as the data is entered, field by field.
Hopefully, the Ajax will be finished by the time the last field is completed and the form submitted.
That at that point, if the Ajax isn't finished, you either have to risk submitting the form without knowing the result of the Ajax validation or fall back to the first option I suggested.
Is making Ajax to sync is the right way?
No. Synchronous HTTP requests from JS are deprecated. Never use them.
You need to either:
Always halt
Always prevent normal form submission.
After you have validated the input, using Ajax, use JS to resubmit the form (without repeating validation).
Validate in advance
Do the validation as the data is entered, field by field.
Hopefully, the Ajax will be finished by the time the last field is completed and the form submitted.
That at that point, if the Ajax isn't finished, you either have to risk submitting the form without knowing the result of the Ajax validation or fall back to the first option I suggested.
answered Jan 2 at 12:02
QuentinQuentin
655k728901054
655k728901054
Thank you very much!
– Toma Tomov
Jan 2 at 12:03
add a comment |
Thank you very much!
– Toma Tomov
Jan 2 at 12:03
Thank you very much!
– Toma Tomov
Jan 2 at 12:03
Thank you very much!
– Toma Tomov
Jan 2 at 12:03
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005950%2fhow-to-prevent-form-from-submitting-with-ajax-validation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown