2012-02-01 24 views
0

我嘗試做與Zend形式,並且知道如何從形式如何使用Zend Framework從窗體中進行選擇?

public function init() 
{ 
    $this->addElement("text","titulo",array(
         "label" => "Titulo" 
        )); 
    $this->setAttrib("id", "enviarNoticia"); 
    $this->setAttrib("class", "FormEnviarNoticia"); 
    $this->setMethod("post"); 
    $this->addElement("textarea","noticia",array()); 
    $this->addElement("submit","Enviar",array()); 
    $this->addElement("multiselect", "categories",array(
         "label"  => "Categories", 
         "required" => false, 
        )); 
} 

如何添加選擇的選項和項目做一個選擇?

+0

我不完全明白什麼是問題,你想達到什麼目的。您能否提供進一步的信息並擴展您的描述? – 2012-02-01 19:15:35

回答

1

而不是試圖從窗體本身獲取數據,您應該從控制器中的模型/數據庫中獲取數據,並將值分配給控制器中的窗體。

// In a controller 

// get the options from your model or database into an array 
$options = array('name' => 'value', 'name2' => 'value2', 'name3' => 'value3'); 

$form = new Application_Form_Form(); 
$form->getElement('categories')->setMultiOptions($options); // set the $options as the options for the categories multiselect 

if ($this->getRequest()->isPost()) { 
    if ($this->form->isValid($this->getRequest()->getPost())) { 
     // form passed validation 
    } 
} else { // form was not submitted 
    // to set default value(s) for the select 
    $form->getElement('categories')->setValue(array('name2', 'name3')); 
} 
相關問題