views:

344

answers:

1

I'want to render:

<input type="text" value="" name="foo[]" />
<input type="text" value="" name="bar[]" />

but Zend_Form_Element require a (string) name, so I need to do:

$this->addElement('text', '1', array(
    'belongsTo' => 'foo'
));

$this->addElement('text', '2', array(
    'belongsTo' => 'bar'
));

but the output is:

<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-2"  type="text" value="" name="bar[2]" />

I can also accept an output like:

<input id="foo-1" type="text" value="" name="foo[1]" />
<input id="bar-1"  type="text" value="" name="bar[1]" />

but Zend_Form_Element rewrite elements with the same name

is there a way to do what I need?

A: 

Hey, you can use this

class MyFooForm extends Zend_Form {
      public function init() {
      $fullNameOpts = array(
               'required'=>false,
               'label'=>'fullName',
               'isArray'=>true,
               'validators' =>  array( array('stringLength', false, array(1, 250) ) )
      );
 $this->addElement('text' ,'fullName',$fullNameOpts);
 // rest of the elements , forms and stuff goes here
      }
 }

And that does creates

<dd id="fullName-element"><input type="text" class="inputAccesible" value="" id="fullName"name="fullName[]"></dd>

It's on Element.php , in Form , line 512 "isArray" check. I'm using a regular zend_form, crossValidation with custom validators and i'm pushing subforms to replicate the main form, 'cause the user can add multiple times the same form. Additionally , I'm too lazy to research custom decorators, i have created one, but it kills subForms and array notation, so i just stick with the regular ones, and that solves it.

I'm at Zf 1.10.

Jorge Omar Vazquez
But generally, when we make use of empty array notation, we have many fields with the same name. If you try to create a new element with the same name expecting a new field fullName[] this does not work
Keyne
Post some code and I'll give you a hand :).I made a lib to deal with this, and my next goal is particularly that.You have one form, and you want to *replicate* only 1 element of the form.
Jorge Omar Vazquez