zend-frameworkzend-form

How to get information from my controller to my form?


For a form I'm trying to add a selectbox which contains a list of items from my database.

My form is situated in /application/forms/News/Edit.php

In my controller I want to fetch this list which I want to use in my form.

How can I add that list from my controller to my form?

This is how my code in Edit.php looks like:

$this->addElement(
   'select',
    'view_status',
    array(
       'label' => 'View status',
       'multioptions' => array(
          //THIS SHOULD BE FILLED WITH DYNAMIC CONTENT FROM MY CONTROLLER
       )
    )
);

Solution

  • You can pass the options of your select in the first param of the form constructor. When initialising the form, Zend_Form look for a set method postfixed by the option name:

    class App_Form_News_Edit extends Zend_Form
    {
        public function setViewStatusOptions($options)
        {
            $this->view_status->setMultioptions($options);
        }
    }
    
    $form = new App_Form_News_Edit(array('viewStatusOptions' => array(..)));