How to prevent form submission based on submitted values

Created by Phil Kurth, Modified on Mon, 04 Jul 2022 at 02:02 PM by Phil Kurth

If you need to stop a form from being submitted based on the submitted values, you can do so as follows:

// Hook into the before_submission action and call wp_die() if the submission has a value we don't want to be submitted.
add_action( 'af/form/before_submission', function ( $form, $fields, $args ) {
  // Only run on a specific form.
  if ( $form['key'] !== 'YOUR_FORM_KEY_HERE' ) {
    return;
  }

  $message = af_get_field( 'YOUR_FIELD_NAME_HERE' );
  if ( $message === 'some unwanted text' ) {
    af_add_submission_error( 'This is a custom error message' );
  }

}, 10, 3 );


By using the af_add_submission_error() function, you will trigger a notice in the UI above the form letting the user know that there was a problem submitting the form.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article