0

I am looping around users with certain conditions so that I can dynamically build a form, this has required a few levels of looping.

1) This is inside a loop, it creates a wrapper fieldset for each user being supplied on each iteration ($uids['userCount'] is a counter)

$form['userToConfirm_' . $uids['userCount']] = array(
    '#title' => t($subsForDisplay[0]['mail']),
    '#type' => 'fieldset',
);
    .....

2) Within that, I loop to contain all the sets of data about that user

Shown here as a fieldset

foreach ($foobar as $foo) {

    $form['userToConfirm_' . $uids['userCount']]
                            [$foo['subset_name']] = array(
        '#title' => t($foo['subset_name']),
        '#type' => 'fieldset',
    );  
        .....

3) Then within that, I pull out the options for each subset

        $form['userToConfirm_' . $uids['userCount']]
                [$foo['subset_name']]
                    [$foo['subset_name'] . 'status'] = array(
            '#title' => t('foo status'),
            '#type' => 'select',
            '#options' => $array,
        );

Based on the following reading: How to place multiple form elements from a loop

and

https://stackoverflow.com/questions/6456671/drupal-form-api-using-foreach-loops-to-build-forms

I think I'm correct in thinking that it's common to unwittingly override the storage every iteration because the keys are not incrementing, or even changing at all, and therefore be left with the last set of data during the form submit.

What I am confused by is why this would be happening when I have already set incrementing key names (for the fieldsets) in my associative array?

Do I need to remove the multi-dimensional part of the $form array, instead of kinda nesting it inside the fieldset? Instead just going straight for every $form key having a unique name?

  • 1
    Won't $uids['userCount'] be the same every time? If it's not indeed a 'count of users' it might be wise to change the variable name to avoid confusion. Otherwise I'd suggest that's why the overriding happens – Clive Mar 27 '13 at 11:22
  • It's a work in progress, currently $uids['userCount'] is incrementing before each wrapper fieldset in '1' - I'll edit it to make it clearer – ja_him Mar 27 '13 at 11:41
  • adding a prefix of $uids['userCount'] to every key name is giving me the output I need in $form_state on submission - it just seems strange that it ignores that I wrapped each nested loop in a key that had an incrementing part – ja_him Mar 27 '13 at 11:45
  • 1
    Sorry if this is way off, but add '#tree' => TRUE to the outer fieldsets and see if that makes things in $form_state['values'] look a bit more like you're expecting – Clive Mar 27 '13 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.