简体   繁体   中英

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?

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

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM