如何从表单中选择Zend Framework?

时间:2012-02-01 19:00:59

标签: forms zend-framework select frameworks

我正在尝试使用zend制作表单,并且我确实知道如何从表单中进行选择

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

如何添加选项和项目?

1 个答案:

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