• Resolved satrya

    (@satrya)


    Hi there,
    I am using forminator_custom_form_submit_before_set_fields to send the form data to API. Here is a simple example.

    add_action(
        'forminator_custom_form_submit_before_set_fields',
        function ($entry, $form_id, $form_data_array) {
            if ($form_id != 12345) {
                return;
            }
    
            // API key & Endpoint
            $endpoint = 'https://endpoint.com/api';
            $body = [
                'name' => 'Satrya',
                'email' => '[email protected]',
            ];
            $body = wp_json_encode($body);
    
            // Send the data to the API
            $response = wp_remote_post($endpoint, [
                'body'        => $body,
                'headers'     => [
                    'Content-Type' => 'application/json',
                ],
                'timeout'     => 60,
                'redirection' => 5,
                'blocking'    => true,
                'httpversion' => '1.0',
                'data_format' => 'body',
            ]);
    
            if (is_wp_error($response)) {
                // Handle error
            } else {
              // Handle success
            }
    
        },
        10,
        3
    );

    If Error or something when wrong
    1. I’d like to display a custom message.
    2. Stop form submit

    Appreciate for any help. Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @satrya

    I hope you are doing good today.

    I pinged our Forminator and SLS Team to review your query. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Thread Starter satrya

    (@satrya)

    Hi Kris,
    Any update for this?

    Hi @satrya

    Can try something like the following snippet. Instead of forminator_custom_form_submit_before_set_fields use forminator_custom_form_submit_errors to add an error and this filter forminator_custom_form_invalid_form_message for showing the error on top of the form.

    add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ) {
    
    		if ($form_id != 12345) {
                return $submit_errors;
            }
    
            // API key & Endpoint
            $endpoint = 'https://endpoint.com/api';
            $body = [
                'name' => 'Satrya',
                'email' => '[email protected]',
            ];
            $body = wp_json_encode($body);
    
            // Send the data to the API
            $response = wp_remote_post($endpoint, [
                'body'        => $body,
                'headers'     => [
                    'Content-Type' => 'application/json',
                ],
                'timeout'     => 60,
                'redirection' => 5,
                'blocking'    => true,
                'httpversion' => '1.0',
                'data_format' => 'body',
            ]);
    
            if (is_wp_error($response)) {
                $submit_errors[]['submit'] = __('Your error message', 'forminator');
                $GLOBALS['api_error'] = true;
            } else {
              // Handle success
            	$GLOBALS['api_error'] = false;
            }
    return $submit_errors;
    },15,3);
    
    add_filter( 'forminator_custom_form_invalid_form_message', 'wpmudev_invalid_form_error_response', 10, 2 );
    function wpmudev_invalid_form_error_response( $invalid_form_message, $form_id ){
    	if( $form_id != 12345 ) {
    		return $invalid_form_message;
    	}
    
    	
    
    	if ( $GLOBALS['api_error'] ) {
    		$invalid_form_message = __( 'Your error message', 'forminator' );
    	}
    	
    	return $invalid_form_message;
    }

    Kind Regards,
    Kris

    Thread Starter satrya

    (@satrya)

    Hi Kris,
    If error, will it stop the form submitting the data?

    • This reply was modified 3 years, 9 months ago by satrya.
    Thread Starter satrya

    (@satrya)

    Nevermind, it is working. Thank you Kris!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Forminator to API Handle Errors’ is closed to new replies.