modify entry field before saving
-
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
-
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,
ZaferHi 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,
ZaferThanks, i’ll try it and let you know
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 modifiedadd_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: +212611223344and the stored value is shown as follows
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,
DmytroHi @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- This reply was modified 9 months ago by Laura - WPMU DEV Support.
I did the change but it had no effect
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,
KrisHi @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
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
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
- The topic ‘modify entry field before saving’ is closed to new replies.