Zend_Form - Array based elements?
Asked Answered
F

2

16

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

<input type="text" name="element[1]" value="" />
<input type="text" name="element[2]" value="" />
// etc...
Felicity answered 2/1, 2009 at 2:31 Comment(0)
B
24

You can either use subforms:

$form = new Zend_Form();

$subForm = new Zend_Form_SubForm();
$subForm->addElement('Text', '1')
        ->addElement('Text', '2');

$form->addSubForm($subForm, 'element');

Or you should also be able to use setBelongsTo() on the form elements (untested):

$form = new Zend_Form();
$form->addElement('Text', '1', array('belongsTo' => 'element'))
     ->addElement('Text', '2', array('belongsTo' => 'element'));
Burdick answered 2/1, 2009 at 8:7 Comment(3)
Second, form seems to be more clean and straight-forward, and works ok (tested).Belted
If anyone has issues with validation, getValue() etc - see this resolved ticket on ZF tracker: framework.zend.com/issues/browse/ZF-2563Belted
I'd go with sub-forms, belongsTo caused me all sorts of grief, because I wanted to use repeating sets of composite fields (example: street and postal address fields which I want to share identically named sub-fields). Only use belongsTo for the most basic of field grouping, otherwise go with subforms and save yourself the hassle.Rectrix
B
2

I contend that setBelongsTo is of substandard quality, as one is unable to set default values. And so, at the present time, there's no reasonable way to achieve your objective.

Buttock answered 21/6, 2011 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.