1

我知道這個問題已經回答了here。但這不適合我。 通過使用插件加載器生成的表單:如何將params從控制器傳遞給Zend_Form?

$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; 
    } 
} 

,我可以看到load方法只返回類的名字,所以new $formClass();等於new Form_Payment()偏偏這個ISN不接受參數?

+1

'Zend_Registry :: get('formloader')'這是一個自定義插件嗎?你也可以發佈該代碼嗎? –

+0

你沒有通過任何params。你想傳遞給表單的是什麼? – Phil

+0

@NandakumarV''Zend_Registry :: get('formloader')'加載Form_Payment @Phil我編輯了上面的代碼,所以現在應該更清楚了 – tecmec

回答

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

} 
-1

您可以添加自定義功能,以您的表單類像

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

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

和instanciating形式控制器

$form = new $formClass(); 
$form->doSome(); 
+0

或者你知道,只需調用'$ form-> setAction('other /行動「)'。 'Zend_Form :: setAction()'是一個公共方法 – Phil

+0

我的意思是一般情況下,如果表單已經有公共方法 - 您應該使用它們:) – Vadyus

+0

@Vadyus但必須知道init方法的值 – tecmec

相關問題