Adding dynamic amount of subforms to Zend_Form
<p>I'm creating my form using JS - it will have a structure of:</p>

<pre><code>&lt;form&gt;
[other input fields here]

// these rows added via JS
&lt;ul&gt;
&lt;li&gt;
&lt;input name="field[0][id]"&gt;
&lt;input name="field[0][data]"&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;input name="field[1][id]"&gt;
&lt;input name="field[1][data]"&gt;
&lt;/li&gt;
...
&lt;/form&gt;
</code></pre>

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

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

<hr>

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

<pre><code>foreach($values['field'] as $index =&gt; $values)
{
    $index_form = new Zend_Form_SubForm();

    $values_form = new Zend_Form_SubForm();

    $data = new Zend_Form_Element_Text('data');

    $values_form-&gt;addElement($data);

    $index_form-&gt;addSubForm($values_form, $index);
    $this-&gt;addSubForm($index_form, "field");
}
</code></pre>

<p>POST:</p>

<pre><code>array (size=5)
  -5 =&gt; 
    array (size=1)
      'data' =&gt; string '13' (length=2)
  -4 =&gt; 
    array (size=1)
      'data' =&gt; string '10' (length=2)
  -3 =&gt; 
    array (size=1)
      'data' =&gt; string '11' (length=2)
  -2 =&gt; 
    array (size=1)
      'data' =&gt; string '12' (length=2)
  -1 =&gt; 
    array (size=1)
      'data' =&gt; string '15' (length=2)
</code></pre>

<p>getValues():</p>

<pre><code>array (size=1)
  1 =&gt; 
    array (size=4)
      'data' =&gt; string '15' (length=2)
</code></pre>

<p>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).</p>