Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to populate a custom zend_form_element with $form->populate() and $form->isValid()
    primarykey
    data
    text
    <p>I've created a custom Zend_form_element following a tutorial to have some custom input data. Everything is more or less okay, it displays correctly and so on. What i need to do is to populate it when i display the form for updating or when displaying the form when it does not pass validation. Here is the code for my custom element:</p> <pre><code>class ZC_Form_Element_TabellaRendite extends Zend_Form_Element_Xhtml { public $helper = "tabellaRenditeElement"; private $_data; // the second paramater was added by me, i'll explain why below function __construct($spec, $data = null){ $this-&gt;_data = $data; parent::__construct($spec); } public function setValue() { } public function getValue() { return $this-&gt;_data; } } </code></pre> <p>And here is the helper function</p> <pre><code> class ZC_View_Helper_TabellaRenditeElement extends Zend_View_Helper_FormElement { protected $html = ''; public function tabellaRenditeElement ($name, $value=null, $attribs = null){ //Here the $attribs are correctly the $specs i passed, the $value only has some value because of the workaround i explain below $helper = new Zend_View_Helper_FormText(); $helper-&gt;setView($this-&gt;view); fb($value, 'value in '); fb($name, 'name'); $options = array('class'=&gt; 'somma','size'=&gt; 4); $optionsReadonly = array('readonly' =&gt; 1, 'class'=&gt; 'totale', 'size'=&gt; 4); if (!$attribs['modificabile']){ $options['readonly'] = 1; } $this-&gt;html .= " &lt;table class='display datatablesRendite' id='tableRendite' style='border:1px solid;'&gt; &lt;thead&gt; &lt;tr bgcolor='#B8D3E8'&gt; &lt;th&gt;RENDITA da LOCAZIONI (canone di locazione - manutenzione)&lt;/th&gt; &lt;th&gt;Importo&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt;"; $this-&gt;html .= '&lt;tr&gt;'; $this-&gt;html .= '&lt;td&gt;LOCALI COMMERCIALI - IMPIANTI SPORTIVI&lt;/td&gt;'; $this-&gt;html .= '&lt;td&gt;'; $this-&gt;html .= $helper-&gt;formText("renditaImpianti",$value['renditaImpianti'], $options); $this-&gt;html .= '&lt;/td&gt;'; $this-&gt;html .= '&lt;/tr&gt;'; $this-&gt;html .= '&lt;tr&gt;'; $this-&gt;html .= '&lt;td&gt;LOCALI COMMERCIALI - AGGIUNTI (servizio di ristorazione)&lt;/td&gt;'; $this-&gt;html .= '&lt;td&gt;'; $this-&gt;html .= $helper-&gt;formText("renditaAggiunte", $value['renditaAggiunte'], $options); $this-&gt;html .= '&lt;/td&gt;'; $this-&gt;html .= '&lt;/tr&gt;'; $this-&gt;html .= '&lt;/tbody&gt;&lt;/table&gt;'; return $this-&gt;html; } } </code></pre> <p>I'm totally new to the zend_framework and this is obviously wron, as you se i added a second parameter called data to the __construct of the element: i've done this because when i create my form and pass the data to populate it i don't know how to have it passed to the helper. So i made the workaround of passing the data directly to the custom zend_form_element in the constructor and (i don't know why) it works. </p> <p>This means that if i do </p> <pre><code>$form = new My_Form(); $form-&gt;populate($data); </code></pre> <p>or </p> <pre><code>$form = new My_Form(); $form-&gt;isValid($_POST); </code></pre> <p>The $value in the helper is empty.</p> <p>So in the init() function of the form i pass the $data to the custom element like this:</p> <pre><code>$myCustomElement = new My_custom_element($specs, $data); </code></pre> <p>and i pass the data to the form on creation</p> <pre><code>$form = new My_Form($data);//this way i pass the data to populate custom elements $form-&gt;populate($data);//this way i populate all the standard elements </code></pre> <p>Same for isValid()</p> <pre><code>$form = new My_Form($_POST);//this way i pass the data to populate custom elements $form-&gt;isValid($_POST);//this way i populate all the standard elements </code></pre> <p>This way everythings work ok, but i'm sure this is pretty wrong: my boss finally gave me half a day to refactor the code and so i want to poulate bothe the custom and standard fields with $form->populate() and $form->isValid().</p> <p>P.S. maybe i got everything wrong and this is not the correct way to do what i wanted to do: feel free to point out the correct way, i'm new to the framework and i i hadn't the time to understand it fully.</p>
    singulars
    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