Skip to main content
Kinetic Community

How to prevent page submission on error of a beforeSubmit event

When using custom events on the beforeSubmit action of a page, it is often advantageous to add a "catch all" for potential errors and to prevent the page from submitting. For example, if I have a custom event on beforeSubmit that copies multiple elements from the page and stores them in a hidden answer field, and that event errors, I don't want to page to submit (indicating to the user that the request was successful) with a blank answer field.

Usage

See example below.

Example

To prevent the page from submitting when there is an error, the Custom Code can be wrapped in a Javascript try/catch block.  For example:

   /* Note that this snippit can't use quotation (") or single-line comment (//) characters since it is included inline */
  try {
    /* Custom code goes here; for this example I am doing something that intentionally throws an error */
    var fu = bar;
  } catch(err) {
    /* In this case we want the error message to be displayed with some context in an alert popup */
    alert('There was a problem in the Raise Error beforeSubmit event: ' + err);
    /* Here is the magic word, by ensuring that the event returns false, further processing of the submit event chain halts */
    false;
  }


will pop up a javascript alert with the contents "There was a problem in the Raise Error beforeSubmit event: ReferenceError: bar is not defined".

Configuration

beforeSubmit_CustomEventWithError.png

Result

beforeSubmit_CustomEventWithErrorPopup.png