控制器中的Zend-form以及在特定操作中处理此表单 - 我该怎么做?

时间:2011-05-20 08:12:18

标签: zend-framework zend-form

我的布局我有搜索表单,所以它显示在每个页面上。 它看起来像这样:

<?
$form = new SearchIndexForm();
$form->setAction($this->configuration['BaseUrl']Index/search); 
echo $form; ?>

我想这样:当有人点击提交按钮时,表单会重定向到“索引”控制器上的“搜索”操作,其中处理表单值。

那么,我应该怎么做才能与此相提并论,这将使用post方法从布局中的表单发送数据?

if($this->_request->isPost()){
$formValues = $this->_request->getParams();
    if ($form->isValid($formValues)){
    ...
    }
}

有了上述内容,当我点击提交时,它会让我进入/索引/搜索,但没有任何反应......

表单本身在一个动作中完美运行。

1 个答案:

答案 0 :(得分:1)

看起来你正在做的一切正确,但我认为你应该改变一些代码。

尝试以下代码

 $request = Zend_Controller_Front::getInstance()->getRequest();

//or you can try
//$request = $this->getRequest();

if($request->isPost()){

$form = new SearchIndexForm();

    if ($form->isValid( $request->getPost() ) ){

        echo 'This should output if the form is valid' . PHP_EOL;

    }
}

$request = Zend_Controller_Front::getInstance()->getRequest(); //or you can try //$request = $this->getRequest(); if($request->isPost()){ $form = new SearchIndexForm(); if ($form->isValid( $request->getPost() ) ){ echo 'This should output if the form is valid' . PHP_EOL; } }

我不喜欢像在$ this-&gt; _request中那样直接访问变量,因为Zend可能必须对变量做一些事情以使它们“正确”。我知道有时候什么也没做,但比抱歉更安全。除非你对此持肯定态度,否则我通常不会对此持肯定态度,除非我真的看过代码。

相关问题