Stop form submission and return custom error message

Hi
How can you stop the form submission and return a specific error message to the user based a function which hooks into gform_pre_submission?

Many thanks

You should probably be using gform_validation or gform_field_validation:

or

If you explain what your end goal is, we should be able to better help you.

I am trying to stop the form submission process (with my custom error message) if for some reason a hooked function isn’t able to complete.

Example:
I have a function which creates a CSV based on the form inputs (hooked to gform_pre_submission). If the file isn’t able to be created (e.g. file permission issue), I wasn’t to stop the form submission process and present an error.

Update: Is it possible to stop the submission process and return an error message, if your check is coming from the gform_notification hook (meaning after validation)?

Reference to my other question - How to attach external file to admin notification?

The submission is already done and entry written when the notification is sent.

Here are some more details.

On form submission a CSV file is created based on certain form fields. This CSV file will have a unique ID in the file name, which is also the ID for the form submission (added to the notification email subject line) e.g. Service ID.
The service ID is a value that is stored in a separate database table, to ensure wordpress doesn’t mess with the value. On every submission the service ID is incremented.

Based on you suggestion (on a previous thread) I have moved all of the function logic to be hooked into gform_notification. I understand the suggestion was based on limited details and therefore might not be the best way to do it.

The gform_notification filter calls my function which does the following:

  1. Retrieves the current service id
  2. Generates the CSV file (based on field inputs)
  3. Attaches the CSV file to the admin notification
  4. Updates the subject line to include the service id (because I cannot store / update an entry here)
  5. Update the service id (using gform_after_submission hook currently, instead of in the above mentioned function; this can be easily changed)

In the rare case that point 2 fails (file is not able to be generated/saved on the server), I wanted to stop the submission process and show the user an error message e.g. Error creating file. Please report this error to the website administrator.

How would I restructure it (the function calls) so that all points are achieved (including file creation error notification), while ensuring a submission and submission id will not get mixed (again in the rare case that there are multiple concurrent submissions?

bump @chrishajer