• Resolved applecarrot

    (@applecarrot)


    Dear Forminator team,

    I’m having some issues with custom code. Not sure if this is the right place to post this?

    We have a form where submissions are sent to an external API. Form submissions are captured as suggested in https://wpmudev.com/forums/topic/other-how-to-capture-the-submitted-form-data/ :

    
    add_action( 'forminator_form_after_save_entry', 'my_form_submit' );
    function my_form_submit( $form_id ) {
    
        if ( $form_id == THE_FORM_ID ) {
    
            $entry = Forminator_Form_Entry_Model::get_latest_entry_by_form_id( $form_id );
    
        }
    }
    

    We also have backend validations, like https://gist.github.com/wpmudev-sls/8ca771c9cde2f583b20280b23291e9cc . Now the problem is that if a validation fails, the entry is not saved, but the hook forminator_form_after_save_entry is called anyway. Which means the last saved entry is processed again, creating a duplicate in the API.

    It seems to me that in Forminator_Front_Action -> save_entry() the action is called unconditionally, is that correct?

    It is confusing that forminator_form_after_save_entry is called even if nothing was saved. Is it a bug or intended behaviour? If it is intended, is there any way to check if a new entry was saved?

    – Marie-Christine

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @applecarrot

    I hope you are doing well.

    It seems a normal behaviour, but I suggest you use this hook instead:

    forminator_form_after_handle_submit

    <?php
    
    add_action( 'forminator_form_after_handle_submit', 'my_callback_function', 20, 2 );
    
    function my_callback_function( $form_id, $response ) {
    
        if( $response['success'] ){
    
            // example how to get the field
            $name = $_POST['name-1'];
    
            // Handle rest of code here
        }
    
    }

    Let us know if this worked for you.
    Best Regards
    Patrick Freitas

    Thread Starter applecarrot

    (@applecarrot)

    Hi @wpmudevsupport12,

    thank you for your quick answer, I’m astonished about how dedicated the WPMU support is.

    I tried your suggestion but it didn’t work for two reasons:

    1. it seems like the hook forminator_form_after_handle_submit is only called when AJAX submit is deactivated? Non-AJAX would be very bad for UX, because all form fields are cleared every time a server side validation fails.

    2. the hook is called even if validations fail and nothing is saved.

    I want to send the form entry to the external API only when the form is fully validated and saved. Is there a way to do that?

    Thank you & best regards,
    Marie-Christine

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @applecarrot,

    In that case, you could try using the filter forminator_form_after_save_entry or forminator_form_after_handle_submit and see whether it helps to cover both your queries.

    Please do let us know how that goes.

    Kind Regards,
    Nithin

    Thread Starter applecarrot

    (@applecarrot)

    Hello Nithin @wpmudevsupport11,

    thank you for your answer. Those are the two filters I already tried. Any other options?

    Kind regards,
    Marie-Christine

    Plugin Support Laura – WPMU DEV Support

    (@wpmudev-support8)

    Hi @applecarrot

    Thanks for response!

    Why not do it straight from your own custom validation function “forminator_submit_form_validate_iban”?

    You can check content of $submit_errors right above the

    return $submit_errors;

    line and if there’s no errors, you can call the other part of your code that submits data to external API.

    You can take all the submitted data from $data variable that you have already available there (the very same way as you are using it to read IBAN from it for validation).

    Best regards,
    Adam

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @applecarrot ,

    We haven’t heard from you for a while now, so it looks like you don’t have more questions for us.

    Feel free to re-open this ticket if needed.

    Kind regards
    Kasia

    Thread Starter applecarrot

    (@applecarrot)

    Hi Adam @wpmudev-support8,

    it would be difficult to call the code from the IBAN validation because there are several validators and I don’t know the order in which they are called.

    I already have a workaround that involves comparing the submission timestamp to the current time. I was just looking for a “clean” solution, like a hook that only gets called when a new entry was saved. It seemed like a common use case. If a hook like this doesn’t exist, it’s okay, the workaround will do.

    Regards & thank you all for your time,
    Marie-Christine

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @applecarrot,

    You could also check whether the following filter helps or not:
    forminator_custom_form_submit_errors

    You can use the above filter to implement custom validation, ie use the third filter param to get the current input.

    For example:
    self::$submit_errors = apply_filters( 'forminator_custom_form_submit_errors', self::$submit_errors, self::$module_id, self::$info['field_data_array'] );

    However, the above will only help if you are looking for input values. If you are looking for an entry ID then the above filter won’t help too.

    I have escalated this further to our developer’s attention to check the observation you have mentioned regarding the existing hooks and the limitation you are facing to see if there are any further improvements that are required or not within the plugin side or not.

    Will keep you posted once we have further updates.

    Kind Regards,
    Nithin

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @applecarrot,

    Could you please check and see whether implementing the code as follows helps to cover your needs:

    add_action( 'forminator_form_after_save_entry', 'my_form_submit', 10, 2 );
    function my_form_submit( $form_id, $response ) {
    	if ( $response && is_array( $response ) ) {
    		if ( $response['success'] ) {
    			if ( $form_id == THE_FORM_ID ) {
    				$entry = Forminator_Form_Entry_Model::get_latest_entry_by_form_id( $form_id );
    			}
    		}
    	}
    }
    

    I hope this helps in moving forward.

    Kind Regards,
    Nithin

    Thread Starter applecarrot

    (@applecarrot)

    Dear Nithin @wpmudevsupport11,

    Yes, that solved it! Thank you!!

    Wow, this is the third time the Forminator team helped me out. I already gave you a 5 star rating, so I’ll see if I can add a few German translations as a thank you.

    Best Regards,
    Marie-Christine

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Custom code. forminator_form_after_save_entry called if nothing was saved?’ is closed to new replies.