How do I keep a file from uploading when I trigger a beforeSave error?

I have a form that includes the ability for the user to upload a file. Upon submit, I run a couple of validation checks in the beforeSave event. I am able to detect the error and return false and thereby halt the save to my collection from happening. Unfortunately, the upload button has already uploaded the file. Since the upload file is connected to an image field in the collection, the error did not allow the new record (along with the image) to be added to the collection, but the file was still uploaded as an orphan.

How do I keep this from happening? I have inferred from some other discussions that I may have to manually control the upload buttons instead of linking the button to a collection via the provided wix editor, but that seems unfortunate.

Any insight is appreciated.

Currently you can’t delete a file using code, so you have 2 options:

  1. To set an email alert for orphan files and delete them manually once you receive such an email (you can send an detailed email onFileUploaded( ) .

  2. You can start uploading the file after you successfully saved the form, and then update the collection record with the file url. you’ll need to create the correct user experience for that, because it makes the total saving time slower.