Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I found a possible solution, it involves passing an array of <strong>Zend Form elements</strong> to the <strong>Zend Form::__construct()</strong> method. The constructor takes an array of options, one of them is called "<strong>elements</strong>". Have a look at the source code within the Zend Framework library.</p> <p>I coded a new private method within the controller, called buildSurveyForm(). Note : the object, passed as a parameter, is built from a huge SQL query with half a dozen JOIN statements, fetching data from a few tables (surveys, questions, answers, etc.) within the database. One of the public attributes for this class consists of an array of questions, stored as objects (with public methods/attributes as well, etc.). Same for answers. The code for building these classes is pretty trivial and out of topic here.</p> <p>Here's the code within the survey controller. I copy/pasted and edited/dropped a few lines to make it a lot clearer :</p> <pre><code>private function buildSurveyForm(MyApp_Object_Survey $survey) { foreach ($survey-&gt;questions as $question) { $element = new Zend_Form_Element_MultiCheckbox($question-&gt;order); $element-&gt;addMultiOptions($question-&gt;getAnswersLabels()); $element-&gt;setName($question-&gt;order); $element-&gt;setLabel($question-&gt;title); $elements[] = $element; } // Here's the trick : $formOptions = array('elements' =&gt; $elements); $surveyForm = new MyApp_Survey_Form($formOptions); $urlHelper = $this-&gt;_helper-&gt;getHelper('url'); $surveyForm-&gt;setAction($urlHelper-&gt;url(array( 'controller' =&gt; 'survey', 'action' =&gt; 'vote'), 'default' )); $surveyForm-&gt;setMethod('post'); $this-&gt;_forms['survey'] = $surveyForm; return $this-&gt;_forms['survey']; } </code></pre> <p>The <strong>MyApp Survey Form</strong> class only contains a Submit button within the init() method. The dynamically generated elements with the code above are added BEFORE this submit button (which is unexpected, but useful). This class simply extends Zend_Form.</p> <p>Then, within survey controller / view action :</p> <pre><code>public function viewAction() { $surveyModel = $this-&gt;_model['survey']; $survey = $surveyModel-&gt;getFullSurvey($this-&gt;_getParam('id')); $survey = new MyApp_Object_Survey($survey); // Calls above private method : $surveyForm = $this-&gt;buildSurveyForm($survey); $this-&gt;view-&gt;assign(array( 'surveyForm' =&gt; $surveyForm, )); } </code></pre> <p>Adding filters, validators and decorators to form elements is now trivial. My proposal is a bit dirty, but I think it gets the job done. I will add a new proposal if I find something more elegant. Feel free to post different answers/solutions.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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