2012-02-01 7 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

私は問題と達成したいことを正確に理解していません。さらなる情報を提供し、あなたの説明を拡張してもらえますか? –

答えて

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