2
Is there any way to "undo" the method preventDefault()
?
I have this little code that solves this for me, but I was wondering if there’s any more efficient way to do it.
var $form = $('form');
$form.data('working', false);
$form.data('canSubmit', false);
$form.submit(function (e) {
if (form.validations.form($form)) {
if (!$form.data('working')) {
$form.data('working', true);
if ($form.data('canSubmit')) {
// make the post here
} else {
e.preventDefault();
$.get(BASE + 'file.txt', function (response) {
var lastPartnerSent = response;
var nextPartner = '';
switch (lastEmailSent) {
case 'Empresa 1':
nextPartner = 'Empresa 2';
break;
case 'Empresa 2':
nextPartner = 'Empresa 3';
break;
default:
nextPartner = 'Empresa 1';
}
$('input.i_parceiro').val(nextPartner);
$form.data('canSubmit', true);
$form.submit();
});
}
}
} else {
$form.data('working', false);
e.preventDefault();
return false;
}
});
This happens because I have to read and write to a file after validating the fields and only after that post to the page. The post cannot be done by ajax.
The question is basically whether it is possible to divide that form by 3 steps, ie:
1º validate fields, for example name, email, etc (synchronous)
2º depending on the data already validated, make an ajax that will add a "partner" field to the form (asynchronous)
3º submit the form by post