• Resolved mehdismekouar

    (@mehdismekouar)


    Hi,

    I want to use a hook like “forminator_form_before_save_entry” to make some modification to a specific field before passing the entries to be saved

    Thanks

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @mehdismekouar,

    I hope you are doing well today!

    We are checking your request with our SLS (Second Line Support) team to provide a workaround or custom code to achieve this and will inform you accordingly.

    Thanks for the patience while we are looking into this.

    Kind regards,
    Zafer

    Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi again @mehdismekouar,

    Our SLS team provided the following code snippet which could be used as mu-plugin.

    <?php
    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_change_submitted_field_data', 10, 3 );
    function wpmudev_change_submitted_field_data( $entry, $module_id, $field_data_array ) {
        $form_ids = array(141); //Please change the form ID
    	if ( !in_array( $module_id, $form_ids ) ) {
    		return;
    	}
        
        foreach ( $field_data_array as $key => $value ) {
            if ( 'hidden-1' == $value['name'] ) {
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field( 'your new value' );
            }
        }
    
    }

    This is just an example to change the hidden-1 field’s values. Please make sure to change your form ID and you can find more information below on how to use mu-plugins.
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins
    and
    https://wordpress.org/support/article/must-use-plugins/

    Kind regards,
    Zafer

    Thread Starter mehdismekouar

    (@mehdismekouar)

    Thanks, i’ll try it and let you know

    Thread Starter mehdismekouar

    (@mehdismekouar)

    I tried the code but it didn’t work
    I did a small modification with logging, i get the value and i do modify it but it doesn’t get stored modified

    
    add_action( 'forminator_custom_form_submit_before_set_fields', 'phone_number_correction', 10, 3 );
    function phone_number_correction( $entry, $module_id, $field_data_array ) {
    $form_ids = array(3730); //Please change the form ID
    if ( !in_array( $module_id, $form_ids ) ) {
    return;
    }
    
    foreach ( $field_data_array as $key => $value ) {
        if ( 'phone-1' == $value['name'] ) {
            error_log('old phone: ' . $value['value']);
            $new_phone = str_replace('+2120', '+212', str_replace(' ', '', $value['value']));
            error_log('new phone: ' . $new_phone);
            Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = $new_phone;
            $field_data_array[$key]['value'] = $new_phone;
        }
    }
    }

    Here is the logged result for a subscription example:

    [16-Dec-2023 12:00:12 UTC] old phone: +212 611 223344
    [16-Dec-2023 12:00:12 UTC] new phone: +212611223344

    and the stored value is shown as follows

    https://ibb.co/N3nFbHX

    Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @mehdismekouar,

    Thank you for the feedback.

    In order for our techs to check it more thoroughly, could you please also export the form and share it via Google Drive, Dropbox, Pastebin.com, or a similar service.

    This should help us with further troubleshooting your snippet.

    Best Regards,
    Dmytro

    Thread Starter mehdismekouar

    (@mehdismekouar)

    Plugin Support Laura – WPMU DEV Support

    (@wpmudevsupport3)

    Hi @mehdismekouar,

    Hope this message finds you well.

    Before escalating to our developers, I noticed that a function is mission in this line:

    Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = $new_phone;

    It should be

    Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field($new_phone);

    Note that sanitize_text_field() function is required.

    Could you replace it and try again? Here is the final code with the changes:

    add_action( 'forminator_custom_form_submit_before_set_fields', 'phone_number_correction', 10, 3 );
        function phone_number_correction( $entry, $module_id, $field_data_array ) {
        $form_ids = array(3730); //Please change the form ID
        if ( !in_array( $module_id, $form_ids ) ) {
        return;
        }
    
        foreach ( $field_data_array as $key => $value ) {
            if ( 'phone-1' == $value['name'] ) {
                error_log('old phone: ' . $value['value']);
                $new_phone = str_replace('+2120', '+212', str_replace(' ', '', $value['value']));
                error_log('new phone: ' . $new_phone);
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field($new_phone);
                $field_data_array[$key]['value'] = $new_phone;
            }
        }
    }

    Let us know if it works.

    Best regards,
    Laura

    Thread Starter mehdismekouar

    (@mehdismekouar)

    I did the change but it had no effect

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @mehdismekouar

    I pinged our SLS Team once more to fully review this and see what we can do in that matter. We will post an update here as soon as more information is available.

    Kind Regards,
    Kris

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @mehdismekouar,

    Could you please try this snippet and then check whether it works fine:

    <?php
    
    add_action( 'forminator_custom_form_submit_before_set_fields', 'phone_number_correction', 10, 3 );
        function phone_number_correction( $entry, $module_id, $field_data_array ) {
        $form_ids = array(3730); // Please change the form ID.
        if ( !in_array( $module_id, $form_ids ) ) {
            return;
        }
    
        foreach ( $field_data_array as $key => $value ) {
            if ( 'phone-1' == $value['name'] ) {
                $new_phone = str_replace( '+2120', '+212', $value['value'] );
                $new_phone = str_replace(' ', '', $new_phone);
                Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = sanitize_text_field($new_phone);
            }
        }
    }
    
    
    add_filter( 'forminator_prepared_data', 'phone_number_correction_prepared', 10, 2 );
    function phone_number_correction_prepared( $prepared_data, $module_object ){
    	if ( $module_object->id != 3730 ) { // Please change the form ID.
    		return $prepared_data;
    	}
    
    	if ( ! empty( $prepared_data['phone-1'] ) ) {
            $new_phone = str_replace( '+2120', '+212', $prepared_data['phone-1'] );
            $new_phone = str_replace(' ', '', $new_phone);
    		$prepared_data['phone-1'] = sanitize_text_field($new_phone);
    	}
    
    	return $prepared_data;
    }
    
    

    You’ll have to make sure to update 3730 in the above snippet with your from ID.

    Could you please check and see whether the above helps?

    Kind Regards,

    Nithin

    Thread Starter mehdismekouar

    (@mehdismekouar)

    I did and i think it’s working now thanks

    But let me test it for some more days for people to subscribe and i’ll let you know of the final thought

    Thanks again

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @mehdismekouar ,

    We haven’t heard from you for over a week now, so it looks like you no longer need our assistance.

    Feel free to re-open this topic if needed.

    Kind regards
    Kasia

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘modify entry field before saving’ is closed to new replies.