1

我知道这个问题已经在这里回答了。但这对我不起作用。表单是使用 PluginLoader 生成的:

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

付款.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;
   }
}

正如我所看到的,加载方法只返回类名,所以new $formClass();是相等的new Form_Payment(),但为什么这不接受参数?

4

2 回答 2

0

好的,我自己找到了一种方法。我正在寻找一种在 Zend_Form 初始化时注入一些参数的方法。似乎唯一的方法是将参数传递给构造函数——它在 init 方法之前执行。

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
         )) 
   }

}
于 2012-12-17T16:45:30.460 回答
-1

您可以将自定义功能添加到您的表单类中,例如

class Form_Payment extends Zend_Form
{
     public function init()
     {
          $this->setAction('payment/save');
          // and so on
     }

     public function doSome()
     {
          $this->setAction('other/action');
     }
}

并在控制器中实例化表单后调用它

$form = new $formClass();
$form->doSome();
于 2012-12-13T11:58:54.477 回答