AHAH with fieldsets not rebuilding correctly
Hey list, I've got a couple forms that use AHAH functions to generate a drop down of states/provinces based on the user's selection in a country drop down menu. I'm using a very similar method as is found here: http://drupal.org/node/331941 One form works fine with fields as $form['country'] and $form['state']. The other form uses fieldsets and those fields get renamed and lose their hierarchy after the form is rebuilt. So for example, the following fields start out like: $form['guest'][1]['country'] $form['guest'][1]['state'] $form['guest'][1]['zip'] But after the form is rebuilt with form_builder and drupal_render using: $output = drupal_render($form['guest'][1]['state']); $output .= drupal_render($form['guest'][1]['zipcode']); // Render form output as JSON print drupal_json(array('data' => $output, 'status' => true)); The rebuilt fields are logically placed outside of the fieldset and end up as: $form_state['guest'][1]['country'] $form_state['state'] $form_state['zip'] Thoughts on what might cause that? Thanks for any direction. I've struggled with this for hours! Jeff
The Examples project has a complete dependent dropdown example in the AHAH Example. If I were you I would copy it and adapt it. http://drupal.org/project/examples -Randy On Fri, Feb 18, 2011 at 11:05 PM, Jeff Hartman <lists@jeffhartman.com>wrote:
Hey list,
I've got a couple forms that use AHAH functions to generate a drop down of states/provinces based on the user's selection in a country drop down menu.
I'm using a very similar method as is found here: http://drupal.org/node/331941
One form works fine with fields as $form['country'] and $form['state']. The other form uses fieldsets and those fields get renamed and lose their hierarchy after the form is rebuilt.
So for example, the following fields start out like: $form['guest'][1]['country'] $form['guest'][1]['state'] $form['guest'][1]['zip']
But after the form is rebuilt with form_builder and drupal_render using: $output = drupal_render($form['guest'][1]['state']); $output .= drupal_render($form['guest'][1]['zipcode']);
// Render form output as JSON print drupal_json(array('data' => $output, 'status' => true));
The rebuilt fields are logically placed outside of the fieldset and end up as: $form_state['guest'][1]['country'] $form_state['state'] $form_state['zip']
Thoughts on what might cause that? Thanks for any direction. I've struggled with this for hours!
Jeff
-- Randy Fay Drupal Module and Site Development randy@randyfay.com +1 970.462.7450
On Feb 19, 2011, at 12:42 AM, Randy Fay wrote:
The Examples project has a complete dependent dropdown example in the AHAH Example. If I were you I would copy it and adapt it. http://drupal.org/project/examples
-Randy
On Fri, Feb 18, 2011 at 11:05 PM, Jeff Hartman <lists@jeffhartman.com> wrote:
Hey list,
I've got a couple forms that use AHAH functions to generate a drop down of states/provinces based on the user's selection in a country drop down menu.
I'm using a very similar method as is found here: http://drupal.org/node/331941
One form works fine with fields as $form['country'] and $form['state']. The other form uses fieldsets and those fields get renamed and lose their hierarchy after the form is rebuilt.
Randy, Getting ahah to work in general is not the problem. I have it working on one form as I mentioned. The problem form has the values within a fieldset and the fields with updated data are generating new fields instead of replacing the existing values. It may not be the fieldset that is the problem, but I'm supposing it is because that's the main difference between the forms. Jeff
On 2011-02-19, at 10:00 AM, Jeff Hartman wrote:
Getting ahah to work in general is not the problem. I have it working on one form as I mentioned. The problem form has the values within a fieldset and the fields with updated data are generating new fields instead of replacing the existing values. It may not be the fieldset that is the problem, but I'm supposing it is because that's the main difference between the forms.
Are you missing '#tree' on the fieldset element? Or, consider putting it at the top level of your form. --Andrew
On Feb 19, 2011, at 9:21 AM, Andrew Berry wrote:
On 2011-02-19, at 10:00 AM, Jeff Hartman wrote:
Getting ahah to work in general is not the problem. I have it working on one form as I mentioned. The problem form has the values within a fieldset and the fields with updated data are generating new fields instead of replacing the existing values. It may not be the fieldset that is the problem, but I'm supposing it is because that's the main difference between the forms.
Are you missing '#tree' on the fieldset element? Or, consider putting it at the top level of your form.
--Andrew
Yes, this is on the right track. My original form does have #tree included. I have to redefine all the fieldsets *within* the ahah function and that seems to get closer. The names of the fields are still slightly different. Field name before ahah: <input name="details[guests][1][address][state]" ... Field name after ahah: <input name="guests[1][address][state]" ... It's like I'm missing 1 level of depth in the form. Details is not in the $form array as I'm creating the form so I assume that is added when Drupal creates the form. I am not sure how I generate that extra level. It doesn't work if create an empty $form = array('#tree' => true); or $form['details'] = array('#type' => fieldset, '#tree' => true), in the ahah function. Ideas?
On Feb 19, 2011, at 10:09 AM, Jeff Hartman wrote:
On Feb 19, 2011, at 9:21 AM, Andrew Berry wrote:
On 2011-02-19, at 10:00 AM, Jeff Hartman wrote:
Getting ahah to work in general is not the problem. I have it working on one form as I mentioned. The problem form has the values within a fieldset and the fields with updated data are generating new fields instead of replacing the existing values. It may not be the fieldset that is the problem, but I'm supposing it is because that's the main difference between the forms.
Are you missing '#tree' on the fieldset element? Or, consider putting it at the top level of your form.
--Andrew
Yes, this is on the right track. My original form does have #tree included. I have to redefine all the fieldsets *within* the ahah function and that seems to get closer. The names of the fields are still slightly different.
Field name before ahah: <input name="details[guests][1][address][state]" ...
Field name after ahah: <input name="guests[1][address][state]" ...
It works now if I remove the empty $form = array('#tree' => true); at the start of my form. Ahah renders the fields correctly then. :) Thanks!
participants (3)
-
Andrew Berry -
Jeff Hartman -
Randy Fay