Home › Forums › Bug Reports › acf_form() id not being set from $args
Maybe I’m miss understanding this, but on the acf_form() doc page it says:
/* (string) Unique identifier for the form. Defaults to 'acf-form' */ 'id' => 'acf-form',
But looking at the form the id is always ‘post’, even when an id=> is set in the $args.
It looks like in file api-template.php on line 1298, id for the form args is being set to ‘post’ – it was solved for me by setting ‘id’ => $args[‘id’] from the user coded $args list. As well, if no id is set in the user coded $args, then the form is actually give the id ‘acf-form’
Can you supply some code to show where you’ve set the form id and code that shows the wrong id?
acf_form() code looks like this:
<?php $options = array( 'id' => 'data-subsection', 'post_id' => $submission_post->ID, 'fields' => array( 'field_55b1208a40b0e', 'field_5595c1f4a3e3a', 'field_5595c20aa3e3b', 'field_5595c26fa3e3c', ), 'submit_value' => 'Save', 'updated_message' => '', ); ?> <?php acf_form( $options ); ?>
Form tag always has id=’post’, which is not what docs say, so bug/ or doc issue there. Rendered HTML looks like this:
<form id="post" class="acf-form" action="" method="post">
In file api-tmeplate.php starting around line 1277 ( I have been messing w/ this file for testing of ajax submit form, so lines probably don’t exactly match what is in un altered code base) looks like this:
// defaults $args = wp_parse_args( $args, array( 'id' => 'acf-form', 'post_id' => false, 'new_post' => false, 'field_groups' => false, 'fields' => false, 'post_title' => false, 'post_content' => false, 'form' => true, 'form_attributes' => array(), 'return' => add_query_arg( 'updated', 'true', $url ), 'html_before_fields' => '', 'html_after_fields' => '', 'submit_value' => __("Update", 'acf'), 'updated_message' => __("Post updated", 'acf'), 'label_placement' => 'top', 'instruction_placement' => 'label', 'field_el' => 'div', 'uploader' => 'wp' )); $args['form_attributes'] = wp_parse_args( $args['form_attributes'], array( 'id' => 'post', 'class' => '', 'action' => '', 'method' => 'post', ));
I was able to get my $args id to appear by changing:
$args['form_attributes'] = wp_parse_args( $args['form_attributes'], array( 'id' => 'post', 'class' => '', 'action' => '', 'method' => 'post', ));
$args['form_attributes'] = wp_parse_args( $args['form_attributes'], array( 'id' => $args['id'], 'class' => '', 'action' => '', 'method' => 'post', ));
And if no id is supplied in the $args list, then ‘acf-form’ is the form ID, like the docs say it should be.
Okay, I will flag this for the developer to look at. I don’t know if the doc is wrong or there is a bug to be honest.
Either way, it would be great to be able to set the ID of the form. I plan to use several forms on a single page (still not sure if that even works, haven’t had time to test) and I’d like to use the ID to trigger form specific JS.
ahah, went digging through the ACF code. I don’t know what ‘id’ is for, I didn’t even figure out where that ‘id’ value is used. ie. It may not be used at all, I didn’t dig that far.
$args = array( 'id' => 'my-form-id' ); acf_form($args);
But I did figure out how to set the form ID
$args = array( 'form_attributes' => array('id' => 'my-form-id') ); acf_form($args);
Good to know, thanks. But still seems like a bug or at least documentation error.
Thanks for the topic.
The ‘id’ param is not realy used by the HTML form element, but can be used during the acf/pre_save filter or acf/save_post action to check against what form was posted.
the form element uses the ‘post’ id to allow for some WP functionality / styling.
So does that mean you are only envisioning 1 form per page, since the actual form ID is hard (not clear) to change? I’ve not tried multiple forms on a single page, but that is where I”m headed.
Anyways, thanks for the clarification, though def not clear in the acf_form() doc.
Yes, during the save actions, you can obtain the form $args by looking at:
You could review the ‘id’ here to run custom functionality for only specific forms. Perhaps you have 2 forms on the site, but only want to run a function when ‘signup’ is submit.
Understood. In my use case though, there are going to be multiple forms on a single page, in an accordion.
<accordion> <panel> <acf_form()> </panel> <panel> <acf_form()> </panel> </accordion>
It will actually be a 8 step form. Anyways, for this, it is good to know how to set the ID for the form so I can tie specific front end js validation checks to specific forms on the page. John’s research helps with this, but the documentation on the acf_form page, at least to me, is not very clear. Normally if I’m told I’m setting the id of the form in $args, I’d expect to see that in <form id=””>.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.