Correct Way to Check if Public Web Form Is Valid?

Hello, we have a complex public web form with a 6-step wizard and dozens of necessary fields. It submits a payment to third-party processor which must succeed before the form is submitted. Both the payment and form submission must happen from a single "Submit" button.

To ensure success, we need to treat these two transactions as atomic (as much as is possible). With so many fields, it is essential to validate the form before payment is processed.

To do this, we are currently using this type of code:

    if (fd.validate()) {
       // submit payment (async), 
       // await response 
       // if successful, return fd.save()
       // else post error, etc.
    }

The problem is that, while this seemed to work okay for us in testing, it appears to be failing occasionally "in the wild"; we are seeing some payments being made without matching form submissions. One theory I have is that the form is not actually validating on fd.save() after the payment has been submitted.

I have read conflicting information about using fd.validate() vs. fd.isValid. Can you help steer us in the right direction?

Thank you!

Dear @chs-web,
We recommend using fd.validate() but it's asyncronous. So, it should be:

if (await fd.validate()) {
	// submit to payment processor.
}

And then you can place this code inside beforeSave handler.

but it's asyncronous

Oh my. This could explain exactly what we are seeing.

I will mark this as the solution unless we discover otherwise.

Thank you!

1 Like