WAIT: Custom form action returning "success: false"

Browser: Chrome 110
OS: Windows

I have a problem with my implementation of a custom action on form submission. The action code I define runs with no issues, the AJAX response returns a status 200, but its content is always the following:

{ "success": false, "data": { "action": "my_custom_action", "type": "error", "message": "A submiss\u00e3o falhou. Por favor, recarregue a p\u00e1gina e tente enviar o formul\u00e1rio novamente." } }

In my code snippet, I have defined the form result using set_result(), as I thought this would result in a “success: true” response. I doubt this is a bug, I just want to know if there is any specific return that the function I defined should perform.

This is my code, which currently doesn’t have a specific return:

function form_vote_submission_action( $form ) {
    global $wpdb;
	
	// Implementation logic

	error_log("INFO: Finishing. Success.");
    $form->set_result([
 		'action' => 'my_custom_action',
 		'type'    => 'success',
 		'message' => esc_html__('Please check your email to confirm the submission.', 'bricks'),
 	]);
}
add_action( 'bricks/form/custom_action', 'form_vote_submission_action', 10, 1 );

Hi @loutei02 and welcome to the forum :partying_face:.

Can you confirm that you have “custom action” set correctly on your form element: Form Element – Bricks Academy

Let me know,
Matej

Hi @Matej,

Yes, I have done that:
image

I actually managed to make it work by directly calling “wp_send_json_success” inside my code snippet, like this:

function form_vote_submission_action( $form ) {
    global $wpdb;
	
	// Implementation logic

	error_log("INFO: Finishing. Success.");
    
	wp_send_json_success([
        'type' => 'success',
        'message' => esc_html__('Please check your email to confirm the submission.', 'bricks'),
    ]);
}

Although, I’m not sure this is a good practice.

I see. But this is not how it should work though. Can you try to use the same code as in the academy and you disable all plugins and custom snippets?

<?php 
function my_form_custom_action( $form ) {  
  // $fields = $form->get_fields();
  // $formId = $fields['formId'];
  // $postId = $fields['postId'];
  // $settings = $form->get_settings();
  // $files = $form->get_uploaded_files();
  
  // Perform some logic here...

  // Set result in case it fails
  $form->set_result([
    'action' => 'my_custom_action',
    'type'    => 'success', // or 'error' or 'info'
    'message' => esc_html__('Oh my custom action failed', 'bricks'),
  ]);
}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );

Does it work then?
Matej