Skip to main content
Kinetic Community

How to prevent page submission on error of a Submit event

When using custom events on the Submit 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 Submit 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:

   
  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 Submit event: ' + err);
    /* Here is the magic word, by ensuring that we stop the action, further processing of the submit event chain halts */
    action.stop();
  }


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

Configuration

AddPageEvent.png

 

 

Result

SubmitError.png

All that is strictly necessary to prevent page submission is to return false. An alert or display on the screen of some kind is helpful for the user to understand why they clicked submit and did not move forward.