开发者

Zend_Form - multiArray based elements

开发者 https://www.devze.com 2023-04-02 07:03 出处:网络
Using Zend_Form, how would I create form elements like this: <input type=\"text\" name=\"element[0][name]\" value=\"\" /&开发者_开发问答gt;

Using Zend_Form, how would I create form elements like this:

<input type="text" name="element[0][name]" value="" /&开发者_开发问答gt;
<input type="text" name="element[0][text]" value="" />
<input type="text" name="element[1][name]" value="" />
<input type="text" name="element[1][text]" value="" />


You have to use sub forms:

$form = new Zend_Form;

// 'element'
$elements = new Zend_Form_SubForm();
$form->addSubForm($elements, 'element');

And each element is a subform of the 'elements' sub form:

$element = new Zend_Form_SubForm;
$element->addElement('Text', 'name');
$element->addElement('Text', 'text');
$elements->addSubForm($element, '0');

$element = new Zend_Form_SubForm;
$element->addElement('Text', 'name');
$element->addElement('Text', 'text');
$elements->addSubForm($element, '1');

In the controller you can iterate over elements like this:

foreach($form->elements as $element) {
    var_dump($element->getValue('name'));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消