the funny thing is :
<?php
$ids = range(1,10);
$form = new Zend_Form;
// WORKS FINE
foreach($ids as $value)
{
$form->addElement('hidden', 'id_'.$value, array(
'name' => 'id',
'value' => $value,
'isArray' => true,
'required' => true,
));
}
echo $form;
$form = new Zend_Form;
// DOESN'T WORK
foreach($ids as $value)
{
$fieldId = new Zend_Form_Element_Hidden('id');
$fieldId->setIsArray(true);
$fieldId->setValue($value);
$fieldId->setRequired(true);
$form->addElement($fieldId);
}
echo $form;
Matthew Weier O'Phinney ha scritto:
-- Simone Cosci <s.cosci@nextidea.it> wrote (on Wednesday, 15 July 2009, 02:53 PM +0200):What is the way (if exists) to have an array (it means that all fields share the name with [] at the end) of hidden or text form elements ? I want something like <form> <input name="id[]" value="1" /> <input name="id[]" value="2" /> ... </form> ... whats the way ? if I use the same name each element is replaced in the stack ... $elem->setIsArray(true) add [] at the end but each elem is replaced by the next with same name .. (only the last will be rendered) so how ?Create your own element type that will accept an array of values, and then a decorator that will output the various input items. class My_Element_ArrayElement extends Zend_Form_Element { public function init() { $this->addPrefixPath('My_Decorator', 'My/Decorator/', 'decorator'); } public function setValue($value) { $this->_value = (array) $value; } public function loadDefaultDecorators() { if ($this->loadDefaultDecoratorsIsDisabled()) { return; } $decorators = $this->getDecorators(); if (empty($decorators)) { $this->addDecorator('ArrayElement'); } } } class My_Decorator_ArrayElement extends Zend_Form_Decorator_Abstract { public function render($content) { $element = $this->getElement(); $view = $element->getView(); $markup = ''; $name = $element->getName() . '[]'; foreach ($element->getValues() as $value) { $markup .= $view->formHidden($name, $value) . "\n"; } $separator = $this->getSeparator(); switch ($this->getPlacement()) { case 'PREPEND': return $markup . $separator . $content; case 'APPEND': default: return $content . $separator . $markup; } } }
没有评论:
发表评论