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...

2 Answers 2

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'));
Sign up to request clarification or add additional context in comments.

3 Comments

Second, form seems to be more clean and straight-forward, and works ok (tested).
If anyone has issues with validation, getValue() etc - see this resolved ticket on ZF tracker: framework.zend.com/issues/browse/ZF-2563
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.