array input like name="person[]" in zend form
Asked Answered
S

1

8

In normal html, we could have an array field like person[]

<input name="person[]" type="text" />
<input name="person[]" type="text" />
<input name="person[]" type="text" />

As far as I know, Zend_Form doesn't have that. I read another answer that suggested it could be done using a decorator that would add the [] at the right place. This is the code for that specific question

$html = ''; // some code html
$i = 0;
foreach ($element->getMultiOptions() as $value => $label){
    $html .= '<input type="checkbox" '
          .         'name="'.$element->getName().'[]" '
          .         'id="'$element->getName()'-'.$i.'" '
          .         'value="'.$value.'" />';
    $i++;
}
return $html;

This looks like a good start, but I wonder if using a decorator is enough. The values that get returned back have to be read correctly and delivered to the server, then validated on the server side. So is a decorator the wrong idea? Would a custom element make more sense here? I haven't seen a good example that shows how this can be done.

Sabec answered 7/2, 2011 at 14:17 Comment(1)
Check manual: framework.zend.com/manual/en/zend.form.advanced.htmlStallion
L
13

I think that ZF does not allow for creation of individual input text fields named person[], although you could do it for the whole form or a subform. However, it allows for something similar. Specifically, you could create fields named person[0], person[1], etc.

To do this, you could do the following:

$in1 = $this->createElement('text', '0');
$in2 = $this->createElement('text', '1');
$in1->setBelongsTo('person');
$in2->setBelongsTo('person');

This way you could normally attach your validators, filters, etc. to $in1 or $in2 and they would work as expected. In your action, after form validation, you could get an array of the person's input text fields as:

$values = $yourForm->getValues();
var_dump($values['person']);

Interestingly, the following will NOT work:

$in1 = $this->createElement('text', 'person[0]');
$in2 = $this->createElement('text', 'person[1]');

Hope this will help you.

Limbo answered 7/2, 2011 at 15:25 Comment(2)
I've seen something similar to your idea while I was searching, but this doesn't work when the number of person[] is dynamic. Take this scenario: you ask the user for emails of his friends but you present him only 1 <input> and a button add one more if he wants to add more. The add one more button is just a javascript that adds additional <input> to the form. The number of times the user clicks add one more is unknown. Without zend_form, I just use person[] for this then send it to the backend for validation. For this field, I loop through all array and check each.Sabec
@jblue. yes, I see this is not that strait forward. But I think there are other possibilities that could be considered. One would be using subforms and marking the ZF subform as an array (look advance ZF usage). Other possibility would be to generate more inputs in ZF, and hide all except the first one. JS than would just unhide the rest if needed. Yet another, would be to create input fields for a ZF dynamicly in php. If JS generates 10 input fields, then, after submit, you add these fields into your Zend Form, with the filters (etc..) you want, populate it and validate as usually.Limbo

© 2022 - 2024 — McMap. All rights reserved.