Zend Framework, Zend_Form_Element how to set custom name?

Posted by ikso on Stack Overflow See other posts from Stack Overflow or by ikso
Published on 2010-04-02T15:52:52Z Indexed on 2010/04/02 16:33 UTC
Read the original article Hit count: 853

Hello, I have form, where some fields are looks like rows, so I can add/delete them using JS. For example:

Field with ID=1 (existing row)

<input id="id[1]" type="text" name="id[1]" value="1" />
<input id="name[1]" type="text" name="name[1]" value="100" />

Field with ID=2 (existing row)

<input id="name[2]" type="text" name="name[2]" value="200" />
<input id="name[2]" type="text" name="name[2]" value="200" />

new row created by default (to allow add one more row to existing rows)

<input id="id[n0]" type="text" name="id[n0]" value="" />
<input id="name[n0]" type="text" name="name[n0]" value="" />

new row created by JS

<input id="id[n1]" type="text" name="id[n1]" value="" />
<input id="name[n1]" type="text" name="name[n1]" value="" />

So than we will proceed form, we will know what rows to update and what to add (if index starts with "n" - new, if index is number - existent element).

I tried subforms... but do I have to create subform for each field? If I use following code:

$subForm = new Zend_Form_SubForm();
$subForm->addElement('Text', 'n0');
$this->addSubForm($subForm, 'pid');       
$subForm = new Zend_Form_SubForm();
$subForm->addElement('Text', 'n0');
$this->addSubForm($subForm, 'name');

What is the best way for this?

1) Use subforms?

2) Extend Zend/Form/Decorator/ViewHelper.php to use names like name[nX]?

3) Other solutions?


© Stack Overflow or respective owner

Related posts about zend-framework

Related posts about zend-form