(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;
}
}
}
--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/
没有评论:
发表评论