I have the following code:

foreach ($emails as $key=>$email)
	{	
		$form["email_$key"] = array(
			'#type'=>'textfield',
			'#title'=>"Email $key",
			'#default_value'=>$email
			);
	}

It is supposed to cycle through the $emails array and generate a textfield for each. This it does. In fact, everything is fine until the form is submitted, then in my _submit function, when I look at $form_values, I can't access these values--they simply aren't there.

It only happens when the form fields are dynamically generated through a foreach or a while loop or something like that. If I hand-code them, it works fine.

For example, you'd think the following would generate the same form elements:


//Dynamic generation
while ($i <= count($emails)) 
	{
		$form['addresses']["address_$i"] = array(
			'#type'=>'textfield',
			'#title'=>"Email $i",
			'#default_value'=>$emails[$i]
			);
		$i++;
	}

//Manual generation
	$form['addresses']["address_test_0"] = array(
			'#type'=>'textfield',
			'#title'=>"Email 0",
			'#default_value'=>$emails[0]
			);
	$form['addresses']["address_test_1"] = array(
			'#type'=>'textfield',
			'#title'=>"Email 1",
			'#default_value'=>$emails[1]
			);

Well they generate the same exact elements as evidenced by this var_dump of $form:

Array
(
    [op] => Array
        (
            [#type] => hidden
            [#value] => send_mail
        )

    [addresses] => Array
        (
            [#type] => fieldset
            [#tree] => 1
            [address_0] => Array
                (
                    [#type] => textfield
                    [#title] => Email 0
                    [#default_value] => foo@bar.com
                )

            [address_1] => Array
                (
                    [#type] => textfield
                    [#title] => Email 1
                    [#default_value] => foo@bar.com
                )

            [address_2] => Array
                (
                    [#type] => textfield
                    [#title] => Email 2
                    [#default_value] => foo@bar.com
                )

            [address_3] => Array
                (
                    [#type] => textfield
                    [#title] => Email 3
                    [#default_value] => 
                )

            [address_test_0] => Array
                (
                    [#type] => textfield
                    [#title] => Email 0
                    [#default_value] => foo@bar.com
                )

            [address_test_1] => Array
                (
                    [#type] => textfield
                    [#title] => Email 1
                    [#default_value] => foo@bar.com
                )

        )

So while these look (structurally) identical, when FAPI processes the form, here's the contents of the $form_values array:

array(9) { ["op"]=> string(6) "Submit" ["addresses"]=> array(3) { ["address_0"]=> string(19) "foo@bar.com" ["address_test_0"]=> string(19) "foo@bar.com" ["address_test_1"]=> string(17) "foo@bar.com" } ["newsletter"]=> string(2) "45" ["desired_action"]=> string(3) "add" ["email_test_0"]=> string(19) "foo@bar.com" ["email_test_1"]=> string(17) "foo@bar.com" ["submit"]=> string(6) "Submit" ["form_token"]=> string(32) "81e29f92a47d7af15863d1d722eb2ae0" ["form_id"]=> string(31) "my_module_send_mail_form" }

WHY oh why are the rest of the $form['addresses'] elements disappearing? Why is it only when it is generated by a foreach loop and not when hand-coded?

Very strange...any help is greatly appreciated.

PS- I tried throwing #SKIP_DANGEROUS_CHECK on those elements just for fun but that didn't fix it.

Comments

scott_cooley’s picture

Status: Active » Closed (fixed)

Turns out that the var that contained the data to generate the form elements from was getting lost when the form was submitted, so FAPI would check to make sure all the submitted fields were actually present, which they weren't, so it was dropping those fields...sigh.

Fixed it though.

glennnz’s picture

How?