Skip to content

Instantly share code, notes, and snippets.

@VictorPietro
Created January 24, 2025 13:45
Show Gist options
  • Save VictorPietro/3b683f447cc6a1f1ddfbc2bb53bab142 to your computer and use it in GitHub Desktop.
Save VictorPietro/3b683f447cc6a1f1ddfbc2bb53bab142 to your computer and use it in GitHub Desktop.
Set JetFormBuilder form statuses. All credits to @girafffee
<?php
use Jet_Form_Builder\Exceptions\Action_Exception;
add_action( 'jet-form-builder/custom-action/test_action', function ( $request, $handler ) {
if ( empty( $request['age'] ) ) {
/**
* You can use one of the default statuses
* 'success' => 'Form successfully submitted.',
* 'failed' => 'There was an error trying to submit form. Please try again later.',
* 'validation_failed' => 'One or more fields have an error. Please check and try again.',
* 'captcha_failed' => 'Captcha validation failed',
* 'invalid_email' => 'The e-mail address entered is invalid.',
* 'empty_field' => 'The field is required.',
* 'internal_error' => 'Internal server error. Please try again later.',
* 'upload_max_files' => 'Maximum upload files limit is reached.',
* 'upload_max_size' => 'Upload max size exceeded.',
* 'upload_mime_types' => 'File type is not allowed.',
*/
throw new Action_Exception( 'empty_field' );
}
if ( absint( $request['age'] ) < 18 ) {
throw ( new Action_Exception( 'Your age is less than necessary' ) )->dynamic_error();
}
/**
* If all checks are passed, you just need to do Nothing,
* so that the form would continue its work or successfully complete it.
*
* In rare cases, you can interrupt the execution of the form with a successful status.
*/
if ( 199 === absint( $request['age'] ) ) {
// or throw new Action_Exception( 'success' );
throw ( new Action_Exception( 'Lucky!' ) )->dynamic_success();
}
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment