调用多个页面时,zend表单不起作用

时间:2012-05-15 09:44:02

标签: zend-framework

我有一个控制器动作

public function pagedetailAction(){

    $id  = $this->_getParam('id',0);
    $detail = new Application_Model_Page();
    $this->view->detail = $detail->findArr($id);
    //CALLING FORM WITH OTHER CONTENTS
    $form = new Application_Form_Request();
    $this->view->form = $form;

}

要处理我有此功能的表单

public function submitrequestAction(){

    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {


            $newsadd = new Application_Model_Request($formData);
            $newsadd->set_ID(null);
            $newsadd->save();

            $this->_helper->flashMessenger->addMessage(array('success'=>'Message Sent'));
            $this->_redirect('/index/frontmsg');
        } else {
            $form->populate($formData);
        }

    }

}

当我点击提交时,我收到错误

Notice: Undefined variable: form in E:\xampp\htdocs\sandbox1\application\controllers\IndexController.php on line 84

Fatal error: Call to a member function isValid() on a non-object in E:\xampp\htdocs\sandbox1\application\controllers\IndexController.php on line 84

请帮助我,以便我可以通过此表单将值发布到数据库。感谢

2 个答案:

答案 0 :(得分:1)

使用此

 public function submitrequestAction(){

    if ($this->getRequest()->isPost()) {
        $form = new Application_Form_Request();//added this
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {


            $newsadd = new Application_Model_Request($formData);
            $newsadd->set_ID(null);
            $newsadd->save();

            $this->_helper->flashMessenger->addMessage(array('success'=>'Message Sent'));
            $this->_redirect('/index/frontmsg');
        } else {
            $form->populate($formData);
        }

    }

} 

答案 1 :(得分:0)

将$ form初始化添加到submitrequestAction:

$form = new Application_Form_Request();