views:

32

answers:

1

Hello !

I want to be able to add to Zend_Form many Zend_Form_Element_Select.

I've got some loop in My_Form_Selects extends Zend_Form with

$element = $this->createElement('Select', 'element[]');
$this->addElement($element);

but it creates only one select element (Zend_Form ignores [] in element's name).

How should I do this ?

+1  A: 

Have you tried:

$foo = new Zend_Form_SubForm();
$foo->setElementsBelongTo('foo')
    ->setElements(array(
        'bar' => 'text',
        'baz' => 'text'
    ));
echo $foo;

which results in HTML something like:

<input type="text" name="foo[bar]" id="foo.bar" value="" />
<input type="text" name="foo[baz]" id="foo.baz" value="" />

via

From the manual:

Zend_Form::setIsArray($flag): By setting the flag TRUE, you can indicate that an entire form should be treated as an array. By default, the form's name will be used as the name of the array, unless setElementsBelongTo() has been called. If the form has no specified name, or if setElementsBelongTo() has not been set, this flag will be ignored (as there is no array name to which the elements may belong).

You may determine if a form is being treated as an array using the isArray() accessor.

Zend_Form::setElementsBelongTo($array): Using this method, you can specify the name of an array to which all elements of the form belong. You can determine the name using the getElementsBelongTo() accessor.

fabrik