2011-08-03 67 views
1

我有這個問題:我想傳遞給我的表單一個參數,因爲我需要它來完成一個選擇。Zend表單:將var從控制器傳遞到表單

這裏是我的代碼

控制器:

$p1 = $this->getRequest()->getParam ('1'); 
    $p2 = $this->getRequest()->getParam ('2'); 
    $utentepost = new Application_Model_myMapper(); 
    $data = $utentepost->populateFormInsert($p1, $p2); 
    $form = new Application_Form_myForm(); 
    $form->populate($data); 
    ... 

形式

公共職能的init(){

$this->setMethod('post'); 

    $this->addElement('text', 'p1', array()); 
    $this->addElement('text', 'p2', array()); 

    $this->addElement('select', 'sede_id', array(
      'label'   => 'Sede', 
      'required'  => true, 
      'multiOptions' => $this->_setSelect($p1), 
     )); 
     .... .... 
    protected function _setSelect($p1) { 
     ... call model/mapper to execute sql query 
    } 

感謝

+1

我在這裏回答了類似的問題有關傳遞變量的形式:http://stackoverflow.com/questions/ 6607915/Zend的外形元件不同渲染-上不同動作/ 6609081#6609081 –

回答

1

你可以做到以下幾點:如果您在表單中添加定義

的構造「父:: ..」:

public function __construct($options = null) 
{ 
    parent::__construct($options); 
} 

現在通過attribs爲陣到您的窗體:

$form = new Application_Form_myForm(array('p1' => $p1, 'p2' => $p2)); 

表單裏面:

protected function _setSelect() { 
    $p1 = $this->getAttrib('p1'); 
    ... call model/mapper to execute sql query 
} 
相關問題