zend-frameworkzend-formzend-controllerzend-configzend-loader

How to pass params from controller to Zend_Form?


I know this question is already answered here. But this doesnt work for me. The Form is generated by using the PluginLoader:

$formClass = Zend_Registry::get('formloader')->load('Payment');
$form = new $formClass(array('someval' => $my_arr));

Payment.php:

class Form_Payment extends Zend_Form
{

   protected $_someval = array();

   public function init()
   {
      $this->setAction('payment/save');
      //....
      $this->addElement('multiCheckbox', 'store_id', array('label' => 'Someval:', 'required' => true, 'multiOptions' => $this->getSomeval()))
   }

   public function setSomeval($someval) {
      $this->_someval = $someval;
   }

   public function getSomeval() {
      return $this->_someval;
   }
}

As I can see the load method only returns the class name, so new $formClass(); is equal new Form_Payment() but why this isn't accept params?


Solution

  • Ok I found a way by myself. I was looking for a way to inject some params while my Zend_Form was initialised. It seems the only way for this is to pass the params to the constructor - which is executed before the init method.

    class Form_Payment extends Zend_Form
    {
    
       private $_someval;
    
       public function __construct(array $params = array())
       {
           $this->_someval = $params['someval'];
           parent::__construct();
       }
    
       public function init()
       {
          $this->setAction('payment/save');
          //....
          $this->addElement('multiCheckbox', 'store_id', 
             array('label' => 'Someval:', 
                   'required' => true, 
                   'multiOptions' => $this->_someval // passed params now available
             )) 
       }
    
    }