Note that there are some explanatory texts on larger screens.

plurals
  1. POZend form/subform partial render
    primarykey
    data
    text
    <p>I am building a form using Zend_Form ... i need to add some fields to the form dynamically (jquery/ajax) ... i created a subform for each set of fields that need to be created at runtime ... to get my elements in an array with correct names like <code>subform1[index][element]</code> , i added 2 more subform : subform1 : a wrapper rendered as a field set subform2 : a container that plays the <code>index</code> part of the names elements subform : this is main subform that holds the elements</p> <p>the subform2 and elements part gets repeated for each set of new elements ... </p> <p>now everything works fine when form is created and rendered in the view , i get the correct names (<code>subform1[index][element]</code>) ...</p> <p>but when i use ajax to generate new elements i don't know how to render the form and get the subform2 part with correct names ...</p> <p>this is the function that creates the subforms :</p> <pre><code> private function makeSubForm($id, $legend) { $wrapper = new Zend_Form_SubForm(); $wrapper-&gt;setOptions(array('legend' =&gt; $legend, 'class' =&gt; 'ui-corner-all multi_value')) -&gt;setIsArray(true); $array = $this-&gt;_getArray($id); //-------------------------LOOP-------------------------- $form_name = "Application_Form_Employ_$id"; // var_dump($array); foreach ($array as $key =&gt; $value) { $form = new $form_name(); $container = new Zend_Form_SubForm(); $container-&gt;setOptions(array('class' =&gt; 'subform')) -&gt;setIsArray(true) -&gt;addSubForm($form, 'xxxxx'); $wrapper-&gt;addSubForm($container, $key); } Zend_Registry::get('session')-&gt;{$id} = $array; //---------------------------------------------------------- $this-&gt;addSubForm($wrapper, $id); } </code></pre> <p>and this is my action method :</p> <pre><code> public function newElementAction() { $this-&gt;_disableView(); $id = $this-&gt;getRequest()-&gt;getParam('id'); $form = new Application_Form_Employment(); $params = array(); $keys = Zend_Registry::get('session')-&gt;{$id}; $lastKey = end(array_keys($keys)); $keys[++$lastKey] = array(); $params[$id] = $keys; // var_dump($params); $form-&gt;create($params); // $form-&gt;render(); $this-&gt;_response-&gt;appendBody($form-&gt;getSubForm($id)-&gt;getSubForm($lastKey)); } </code></pre> <p>with this code i get <code>subform2[element]</code> as my names ... but if i uncomment <code>$form-&gt;render();</code> i get <code>subform1[subform2][subform1][subform2][element]</code></p> <p>so how can i get the second subform's HTML with correct names ?</p> <hr> <p>well i found a hack to get what i want but i am looking for a better way ...</p> <pre><code>public function newElementAction() { $this-&gt;_disableView(); $id = $this-&gt;getRequest()-&gt;getParam('id'); $form = new Application_Form_Employment(); $params = array(); $keys = Zend_Registry::get('session')-&gt;{$id}; $lastKey = end(array_keys($keys)); $keys = array(++$lastKey =&gt; array()); $params[$id] = $keys; $form-&gt;create($params, $id); $this-&gt;_response-&gt;appendBody($form-&gt;{$id}); } </code></pre> <p>i created only the last child and outputed the parent and in the client side removed the parents markup before inserting into page ...</p> <p>there has to be a solid way to just render the subform with its parents indexes intact !!??</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload