Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes there is.</p> <p>most credits go to jeremy kendall see <a href="http://www.jeremykendall.net/2009/01/19/dynamically-adding-elements-to-zend-form/" rel="nofollow">http://www.jeremykendall.net/2009/01/19/dynamically-adding-elements-to-zend-form/</a></p> <p>The way i solved it is by doing a jquery/ajax call to a action which adds get a form element something like:</p> <pre class="lang-js prettyprint-override"><code>$.ajax({ type : "POST", url : "/&lt;controller&gt;/addfield/", success : function(newElements) { // Insert new element before the submit button $("#productsNew-submit-element").before(newElements); } }); </code></pre> <p>What this does is it call a action that generates a form element and returns the html which you can then add</p> <pre class="lang-php prettyprint-override"><code>public function addfieldAction() { //use $ajaxContext = $this-&gt;_helper-&gt;getHelper('AjaxContext'); in the init to make it return html via ajax $element = new Zend_Form_Element_Text("extraElement_1"); $element-&gt;setBelongsTo("yourForm"); $element-&gt;setLabel('myElementName'); /* set other stuff like decorators or so */ //now create the html $elements .= $element-&gt;__toString(); $this-&gt;view-&gt;fields = $elements; } </code></pre> <p>After that you will get a new element in your form via ajax</p> <p>Now when you submit you have to do that again but then pre validation</p> <ol> <li>first check if your form has extraElements if so add them again</li> <li>fill the add element with the posted value</li> <li>validate</li> </ol> <pre class="lang-php prettyprint-override"><code>public function saveAction() { function findFields($field) { // return field names that include 'extraElement_' if (strpos($field, 'extraElement_') !== false) { return $field; } } //set all stuff you need especially the form if($this-&gt;getRequest()-&gt;isPost()) { $postValues = $this-&gt;getRequest()-&gt;getPost(); //step 1 $extraFields = array_filter(array_keys(current($postValues)), 'findFields'); //add the element before validation if(count($extraFields) !== 0) { foreach(extraFields as $extraField) { $this-&gt;addFields($postValues[$extraField]); &lt;-- step 2 add the field(s) } } //step 3 validate if($this-&gt;_form-&gt;isValid($postValues)) { //do post validation stuff } else { //show errors } } } </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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