user49438
user49438

Reputation: 909

Adding elements from loop to Zend Form in Zend Framework 2

When adding elements to a Zend 2 Form,

  foreach($headers as $column)
            {
                $checkbox = new Element\Checkbox('checkbox');
                $checkbox->setLabel($column . "");
                $checkbox->setUseHiddenElement(true);
                $checkbox->setCheckedValue("true");
                $form->add($checkbox);
            }

I run into the problem that only the last element is added. From stepping through the code, I know that it is running the loop multiple times, but the add() code of the Zend Form just seems to map the last element rather than add all of the elements. What's the best way to do something like this?

Upvotes: 0

Views: 300

Answers (1)

Ed209
Ed209

Reputation: 821

You need to give each element a unique name rather than just 'checkbox'.

Try this:

$checkbox = new Element\Checkbox($column);

Upvotes: 2

Related Questions