0

I have to first process the data (sending it to another endpoint etc.) and the continue with the normal form submission (posting it 'normal' way to form action url).

If I do 'preventDefault()' form is not submitted in a normal way. If I don't then the data is not processed, because form is submitted immediately. I tried also something like that:

$form.submit(async function (ev) {
  ev.preventDefault();
  const formArr = $(this).serializeArray();
  const formObj = _.mapValues(_.keyBy(formArr, "name"), "value");
  await doSomethingWithFormData(formObj);
  return true;
});

but that doesn't work either. What can be done?

Philipp Chapkovski
  • 1,949
  • 3
  • 22
  • 43
  • 2
    Does this answer your question? [Submit form after calling e.preventDefault()](https://stackoverflow.com/questions/22363838/submit-form-after-calling-e-preventdefault) – Swati Jun 12 '21 at 12:32

1 Answers1

0

You can use something like

$('#form').on('submit' , function (e){
  // add some validations 
  if(validations){
    // Validiated nothing to do  , submits form normally 
   }else{
    // Error in validation 
     e.preventDefault()
    // Show some errors ! 
   }
})
Sanmeet
  • 1,191
  • 1
  • 7
  • 15