Adding dynamic amount of subforms to Zend_Form

Viewed 164

I'm creating my form using JS - it will have a structure of:

<form>
[other input fields here]

// these rows added via JS
<ul>
<li>
<input name="field[0][id]">
<input name="field[0][data]">
</li>
<li>
<input name="field[1][id]">
<input name="field[1][data]">
</li>
...
</form>

I want to validate id fields. As such, for my form, I've constructed a subform field which I'm adding to my form, and... I'm stumped, as, within init() I'm not able to know how many rows there will be.

Is there a way to add an undetermined amount of subforms (akin to declaring a multiselect[] as multivalued, if that makes sense), or, do I have to move the creation of subforms to isValid()?


I've went ahead with moving the functionality to isValid() and the results are... suprising. My code:

foreach($values['field'] as $index => $values)
{
    $index_form = new Zend_Form_SubForm();

    $values_form = new Zend_Form_SubForm();

    $data = new Zend_Form_Element_Text('data');

    $values_form->addElement($data);

    $index_form->addSubForm($values_form, $index);
    $this->addSubForm($index_form, "field");
}

POST:

array (size=5)
  -5 => 
    array (size=1)
      'data' => string '13' (length=2)
  -4 => 
    array (size=1)
      'data' => string '10' (length=2)
  -3 => 
    array (size=1)
      'data' => string '11' (length=2)
  -2 => 
    array (size=1)
      'data' => string '12' (length=2)
  -1 => 
    array (size=1)
      'data' => string '15' (length=2)

getValues():

array (size=1)
  1 => 
    array (size=4)
      'data' => string '15' (length=2)

While I understand why the field names are renamed (and I can live with it) I don't understand why there's only one value in the end (even though each subform has different name).

0 Answers
Related