Use API hook/filter to populate fields before submit?
-
Is there an API hook or filter we could use to populate certain fields on a form when it’s loaded on the page, before submitting?
What we’d like to do is create a ‘confirmation page’ that takes data entered on a previous form and shows it in a textarea field on a new form, where the user can confirm their choices without changing them, and pay an amount previously determined on the first form via a Stripe checkout.
So we’d like to also populate a ‘calculation’ field (preferably, since it’s ‘read only’ or a ‘number’ field to be used for the amount for Stripe.
Or is there another solution for a confirmation/checkout type page?
Thanks for any help.
-
Update: I’ve come up with a possible solution for “finishing” a draft form programmatically. Please let me know if this makes sense, or if there is a better solution. (There is a reason the user can’t finish it themselves, but I won’t get into that.)
I’m thinking we could use the method
Forminator_API::add_form_entry( $form_id, $entry_meta )First, get the data from draft form, then create a new entry with this method — essentially duplicating the draft, which will later expire automatically.
Actually, I will get into the reason I’m looking for such a solution. It has to do with Forminator’s limited field-hiding logic. If it were better, we wouldn’t have to jump through the above hoops.
I want to allow people to pay by check. Ordinarily, they click an option for that, and we hide the Stripe field, no problem. But we have another situation were we need to hide that field, requiring a couple fields to show certain options. Since we can only hide when ALL or ANY options have been set, we can’t do it for both situations. It would be great if there were an OR option for hiding, and we could have, for example, “if A and B are true, hide the field” OR “if C is true, hide the field.”
I would hide it with CSS and unhide with js, but Stripe’s field simply will not respond to that, at least when we’re loading with AJAX. I’ve tried a hundred ways, including with a
MutationObserver(), but it just can’t be hidden, it seems.If you can give me a javascript/CSS solution for hiding it, I could avoid having to do the draft trick idea mentioned. Thanks for any help.
-
This reply was modified 11 months, 4 weeks ago by
jamminjames.
So it doesn’t get lost in all my updates, I asked earlier, but would really love to know, is there a hook that can take a draft save and finish the submission of the form programmatically?
Also, thanks for the filter apply_filters( ‘forminator_field_text_markup’, $html, $field )
What other filters are there for updating other types of fields, like regular text fields (not html or textarea), name, email etc?
Hi @jamminjames ,
If you can give me a javascript/CSS solution for hiding it, I could avoid having to do the draft trick idea mentioned. Thanks for any help.
I’m afraid this will be outside the scope of support we can provide. For that, you’ll need to hire a developer to provide the required custom code for you. WordPress provides a jobs directory here https://jobs.wordpress.net/, if you need further advice about it, feel free to email
[email protected].Subject: ATTN: WPMU DEV support – wp.org
So it doesn’t get lost in all my updates, I asked earlier, but would really love to know, is there a hook that can take a draft save and finish the submission of the form programmatically?
This hook forminator_custom_form_submit_field_data should return data even when the form is saved as draft, but could you please explain what you want to achieve with “finish the submission of the form programmatically”?
Also, thanks for the filter apply_filters( ‘forminator_field_text_markup’, $html, $field )
What other filters are there for updating other types of fields, like regular text fields (not html or textarea), name, email etc?
That filter should actually allow you to map other fields, are you getting issues when trying it?
I hope to hear back from you soon.
Best Regards,
Williams ValerioBy “finish the submission of the form programmatically” I mean, rather than the user going back to their draft via the link and submitting the form manually, can we submit the form via a hook or method?
Hi @jamminjames ,
I hope you’re doing well.
Not exactly, that would actually “kill” the save form feature objetive but if what you want is check those draft ones to get some data from the submissions, you should actually be able to check it directly from the form submissions page – https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#submissions
Could you please confirm if maybe you need something else specifically?
I hope to hear back from you soon,
Best Regards,
Williams ValerioI’m not sure what you mean by ” that would actually “kill” the save form feature objective,” but, to be clear, in our scenario, the user has filled out all the info and saved as draft, and have been redirected to another page. Without going into the details of why we want to do it this way, we would like to save the form for them at that point, so “killing the save form feature” would not be a problem, they won’t need to go back to the draft. Is there no way to do this? Thanks.
The best I could come up with is to copy the form entry data from the draft save with the
Forminator_API::get_entry()method. First, you have to find the entry_id for the draft, with something like:$draft_id = isset($_GET['draft']) ? sanitize_text_field($_GET['draft']) : '';
$table = $wpdb->prefix . 'frmt_form_entry';
$draft_row = $wpdb->get_row( $wpdb->prepare(
"SELECT * FROM $table WHERE draft_id = %s", $draft_id
) );
if ( $draft_row ) {
$entry_id_found = $draft_row->entry_id;
}Then use that to get the entry and copy the fields:
Forminator_API::get_entry($form_id, $entry_id );
$meta_data = $entry->meta_data;
$entry_meta = [];
foreach ($meta_data as $key => $field) {
$entry_meta[] = array(
'name' => $key,
'value' => $field['value']
);
}Then use that to create a new entry with the method:
Forminator_API::add_form_entry( $form_id, $entry_meta );The draft save that we copied from will expire and disappear (whenever the setting for that is, 30 days by default, I think).
Hello @jamminjames
Hope you’re doing well today!
The workaround you’ve mentioned above should do the trick. Could you please confirm if you’ve already applied that and it helped?
Additionally, an easier way will be to use forminator_form_draft_after_save_entry hook and get the latest entry by using the function $entry = Forminator_Form_Entry_Model::get_latest_entry_by_form_id( $form_id );
The above will help you to skip the DB query entirely.
The draft save that we copied from will expire and disappear (whenever the setting for that is, 30 days by default, I think).
Indeed, however, you can modify the retention period for the draft entry. Ref: https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#save-and-continue
Hope this helps.
Kind Regards,
SaurabhYes, I’ve tried it and my strategy works. Thanks for the hook & function, however, my understanding is that if two entries are made at the same time or nearly so, the ‘get_latest_entry_by_form_id()’ function could be wrong. This is why checking for the draft_id is a good idea. Forminator ought to build that into the function, or create another one for it, as it is a foolproof way to get the correct entry. Thanks for all your help, I’ll mark this resolved.
-
This reply was modified 11 months, 4 weeks ago by
The topic ‘Use API hook/filter to populate fields before submit?’ is closed to new replies.