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...
hakre
  • 193,403
  • 52
  • 435
  • 836
leek
  • 11,803
  • 8
  • 45
  • 61

2 Answers2

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'));
workdreamer
  • 2,836
  • 1
  • 35
  • 37
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
  • 1
    Second, form seems to be more clean and straight-forward, and works ok (tested). – Victor Farazdagi Apr 13 '10 at 11:53
  • 2
    If anyone has issues with validation, getValue() etc - see this *resolved* ticket on ZF tracker: http://framework.zend.com/issues/browse/ZF-2563 – Victor Farazdagi Apr 13 '10 at 12:09
  • 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. – starmonkey Nov 02 '11 at 01:58
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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
billy
  • 21
  • 1