2011-03-31 141 views
2

我想將一個變量從控制器傳遞給表單。這如何實現?任何人都可以請幫我解決這個問題。將一個變量從Zend控制器傳遞給zend表格

謝謝。

+1

我們在這裏談論的是什麼樣的變量?一個字段的值?或值來生成一個選擇框,例如? – 2011-03-31 07:41:30

+0

不,它與填充表單元素無關。當我們傳遞$ this-> view-> variableName ='test'時,我指的是類似於視圖的情況; 對於傳遞變量形式的情況,是否有可能做類似的事情? – rasth 2011-03-31 08:01:49

+0

問題不明確。有很多方法可以將變量傳遞給表單。在創建表單時,你需要使用這個變量嗎?你能提供一個你想要這個變量的例子嗎? – Marcin 2011-03-31 10:34:42

回答

3

在您的應用程序的控制器方法。 addMultiOptions($ options);

class OrderController extends Zend_Controller_Action 
{ 
    $options = array(
     'foo' => 'Foo2 Option', 
     'bar' => 'Bar2 Option', 
     'baz' => 'Baz2 Option', 
     'bat' => 'Bat2 Option' 
    ); 

    $form = new Application_Form_PlaceNewOrder(); 
    $form->items->addMultiOptions($options); 

    .... 
} 

在應用程序的表單腳本中。

class Application_Form_PlaceNewOrder extends Zend_Form 
{ 
    public function init() 
    { 
     $items= new Zend_Form_Element_Select('items'); 
     $items->setLabel('Items: *');  
     $items->setValue('foo'); 
     .... 
} 
1

當您創建表單,你可以從你的控制器中添加自定義功能

class Application_Form_Sample extends Zend_Form 
{ 
    public function init() 
    { 
     $this->setMethod('post')->setAction('/sampleController/sampleAction'); 

    $name = new Zend_Form_Element_Text('name'); 
    $name->setLabel('Nome') 
     ->setDescription("Here goes the name") 
     ->setRequired(true); 

     $submit = new Zend_Form_Element_Submit('Submit'); 
     $submit->setIgnore(true); 

     $this->addElement($name) 
     ->addElement($submit); 
    } 
    public function myFunction($param) { 
     //do something with the param 
    } 
} 

你可以調用該函數和「編輯」或「你想要什麼」與形式

+0

我需要傳遞在myFunction()中傳遞的值,即init()函數中的$ param。這可能嗎?我試過但沒有印刷 – rasth 2011-04-06 07:14:36

+0

你打印什麼意思?你想做什麼?你可以在這裏「myFunction」的意思是做你想做的事,你能提供你的代碼並更好地解釋你需要做什麼嗎? – MiPnamic 2011-04-06 22:05:14

4

只是一個活生生的例子:

class Admin_Form_Product extends Admin_Form_Abstract { 

    protected $_categories = array(); 

    protected $_brands = array(); 

    protected $_types = array(); 

    protected $_periods = array(); 

    protected $_stores = array(); 

    public function init() { 
     $this 
      ->addElement('hidden', 'id') 
      ->addElement('text', 'name', array('label' => 'Name:', 'required' => true, 'size' => 50)) 
      ->addElement('multiCheckbox', 'category_id', array('label' => 'Category:', 'required' => true, 'multiOptions' => $this->getCategories())) 
      ->addElement('multiCheckbox', 'store_id', array('label' => 'Stores:', 'required' => true, 'multiOptions' => $this->getStores())) 
      ->addElement('select', 'brand_id', array('label' => 'Brand:', 'required' => true, 'multiOptions' => $this->getBrands())) 
      ->addElement('select', 'type_id', array('label' => 'Type:', 'required' => true, 'multiOptions' => $this->getTypes())) 
      ->addElement('select', 'period_id', array('label' => 'Period:', 'required' => true, 'multiOptions' => $this->getPeriods())) 
      ->addElement('textarea', 'description', array('label' => 'Description:', 'rows' => 12, 'cols' => 50)) 
      ->addElement('text', 'price', array('label' => 'Price ($):', 'required' => true, 'size' => 7)) 
      ->addElement('text', 'quantity', array('label' => 'Quantity:', 'required' => true, 'size' => 7, 'value' => 1)) 
      ->addElement('button', 'submit', array('label' => 'Save', 'type' => 'submit')); 

     $this->category_id->setValidators(array(new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::NULL))); 
     $this->brand_id->setValidators(array(
      new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::NULL) 
     )); 
     $this->price->setValidators(array(new Zend_Validate_Float())); 
    } 

    public function setCategories($categories = array()) { 
     $this->_categories = $categories; 
     return $this; 
    } 

    public function getCategories() { 
     return $this->_categories; 
    } 

    public function setBrands($brands) { 
     $this->_brands = $brands; 
    } 

    public function getBrands() { 
     return $this->_brands; 
    } 

    public function setTypes($types) { 
     $this->_types = $types; 
    } 

    public function getTypes() { 
     return $this->_types; 
    } 

    public function setPeriods($periods) { 
     $this->_periods = $periods; 
    } 

    public function getPeriods() { 
     return $this->_periods; 
    } 

    public function setStores($stores) { 
     $this->_stores = $stores; 
    } 

    public function getStores() { 
     return $this->_stores; 
    } 

    public function prepareDecorators() { 
     $this 
      ->clearDecorators() 
      ->addDecorator(new Zend_Form_Decorator_ViewScript(array('viewScript' => 'forms/product-form.phtml'))) 
      ->addDecorator('Form'); 

     $this->setElementDecorators(array('ViewHelper')); 

     return parent::prepareDecorators(); 
    } 

} 

然後在你控制器,你可以只通過必要瓦爾作爲配置成表單構造:

public function addAction() { 
    $categories = $this->_helper->service('Category')->getCategories(); 
    $brands = $this->_helper->service('Brand')->getBrands(); 
    $types = $this->_helper->service('Type')->getTypes(); 
    $stores = $this->_helper->service('Store')->getStores(); 
    $periods = $this->_helper->service('Period')->getPeriods(); 
    $filter = new Skaya_Filter_Array_Map('name', 'id'); 
    $filterCategory = new Skaya_Filter_Array_ParentRelation(); 

    $form = new Admin_Form_Product(array(
     'name' => 'user', 
     'action' => $this->_helper->url('save'), 
     'method' => Zend_Form::METHOD_POST, 
     'categories' => $filterCategory->filter($categories->toArray()), 
     'brands' => $filter->filter($brands->toArray()), 
     'types' => $filter->filter($types->toArray()), 
     'periods' => $filter->filter($periods->toArray()), 
     'stores' => $filter->filter($stores->toArray()) 
    )); 
    $form->prepareDecorators(); 
    $this->view->form = $form; 
} 
相關問題