• Resolved domelem1231

    (@domelem1231)


    Hi,

    I’m trying to add backend functionality to my Forminator form. Specifically, I want to add an entry to a database when a user clicks the submit button on my Forminator form. I have already managed to do this successfully. However, I’m encountering an issue: if the database insertion fails (which can happen for various reasons), I want to notify the user of this error and prevent the Forminator post from proceeding (e.g., stop the emails configured in Forminator from being sent).

    Here is the code I have so far:

    add_action(
    "forminator_form_after_handle_submit",
    "on_forminator_form_submit",
    10,
    2,
    );

    add_action(
    "forminator_form_ajax_submit_response",
    "on_forminator_form_submit",
    10,
    2,
    );

    function on_forminator_form_submit($response, $form_id) {
    if ($response && is_array($response)) {
    if ($response["success"]) {
    if ($form_id == 963) {
    // Retrieve the content of the Forminator form

    // Insert data into the database

    // If the database insertion is successful

    if (db_insertion_successful()) {
    return $response;
    } else {
    // Return a custom error message to display in the form
    // Prevent the emails configured in Forminator from being sent
    // TODO: Implement this part
    }
    }
    }
    }

    return $response;
    }

    My main issue is that I don’t know how to implement the part where it says “TODO”. I need to return a custom error message to the user and stop the Forminator emails from being sent if the database insertion fails.

    I would greatly appreciate any help with this. Unfortunately, there is very little documentation available on using hooks in Forminator.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Laura – WPMU DEV Support

    (@wpmudevsupport3)

    Hi @domelem1231,

    Hope this message finds you well.

    I want to add an entry to a database when a user clicks the submit button on my Forminator form. I have already managed to do this successfully.

    Forminator has a Database store feature already, and if an error occurs it will return a default error in the front-end and the form will be not submitted, this is in the Settings tab:

    If you are not referring to this feature and you managed a custom code to insert the data in a different way, we will need to check the full code so we can provide you with the right solution.

    if the database insertion fails (which can happen for various reasons), I want to notify the user of this error and prevent the Forminator post from proceeding (e.g., stop the emails configured in Forminator from being sent).

    Guessing you are storing the data in a different way using a custom code the hooks:

    forminator_form_submit_response
    forminator_form_ajax_submit_response

    Runs after the form has been successfully submitted, for either Ajax or PHP methods, resulting in Forminator Pro >> Email Notifications have been sent.

    About the code you shared, after testing it returned an error on my test form, it needs some tweaks:

    add_action("forminator_form_after_handle_submit", "on_forminator_form_submit", 20, 2 );
    add_action("forminator_form_ajax_submit_response",	"on_forminator_form_submit", 20, 2); 
      
    function on_forminator_form_submit($response, $form_id) {
    	if ( 1609 != $form_id ) { // Please change the form ID.
    		return $response;
    	}
    
    	// Retrieve the content of the Forminator form
    
        // Insert data into the database
    
        // If the database insertion is successful
    
        if (db_insertion_successful()) {
    		//add your custom succesful message here, otherwise leave empty 
    		$response['message'] = 'Success custom message';
    	} else {
    		// Return a custom error message to display in the form
    		$response['message'] = 'Failed custom message';
    		// Prevent the emails configured in Forminator from being sent
    		
    		// TODO: Implement this part
    	}
      
    	return $response;
     }

    In case you want to prevent the form submission the code would be this:

    add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ) {
        // Add your form IDs here.
        $form_ids = array( 6 );
        // Change this to the message that you want to show.
        $message = 'You cannot submit now.';
        if ( in_array( intval( $form_id ), $form_ids, true ) ) {
            $result = store_data( $field_data_array );
            if ( $result ) {
                $submit_errors[]['submit'] = $message;
                $GLOBALS['form_error'] = true;
            } else {
                $GLOBALS['form_error'] = false;
            }
        }
        return $submit_errors;
    },15,3);
    
    function store_data( $form_id ){
        // add your custom code to save forminator data
        //$entry_save
        if ( $entry_save ) { //if success return true
            return true;
        }
        return false;
    }
    
    add_filter( 'forminator_custom_form_invalid_form_message', 'wpmudev_invalid_form_error_response_submit', 10, 2 );
    function wpmudev_invalid_form_error_response_submit( $invalid_form_message, $form_id ){
        if ( $form_id != 6 ) { //Change your form ID
            return $invalid_form_message;
        }
    
        if ( $GLOBALS['form_error'] ) {
            $invalid_form_message = __( 'You cannot submit now.', 'forminator' );
        }
    
        return $invalid_form_message;
    }

    Let us know the results or if you require additional information.

    Best regards,
    Laura

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @domelem1231

    We didn’t hear back from you for quite a while already so I’m assuming Laura’s solution helped. I’m marking this as resolved for now but if you still have questions or require further assistance, just let us know and we’ll get back to the case.

    Kind regards,
    Adam

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.