2.确认下form的目录 在application/forms/下 建立一个 Guestbook.phps 作为我们form的类文件,如下: 复制代码 代码如下: <?php class Application_Form_Guestbook extends Zend_Form { public function init() { // Set the method for the display form to POST $this->setMethod("post");//设置提交方式
// And finally add some CSRF protection $this->addElement("hash", "csrf", array( "ignore" => true, )); } }
然后添加一个路由控制文件 applictaion/controller/GuestbookController.php 复制代码 代码如下: <?php class GuestbookController extends Zend_Controller_Action { // snipping indexAction()... public function signAction() { $request = $this->getRequest();//获取接受到得信息 // include_once("../application/forms/Guestbook.php"); 手动加载类,只有不能自动载入时,才需要 $form = new Application_Form_Guestbook;//实例化这个方法
if ($this->getRequest()->isPost()) {//如果是POST传递的结果 if ($form->isValid($request->getPost())) {//判断传递是否有效 $comment = new Application_Model_Guestbook($form->getValues()); $mapper = new Application_Model_GuestbookMapper(); $mapper->save($comment); return $this->_helper->redirector("index"); } }
$this->view->form = $form;//将表单赋值给试图 } }
最后添加一个简单的sign视图文件即可: 地址:application/views/scripts/guestbook/sgin.php 复制代码 代码如下: Please use the form below to sign our guestbook! <?php $this->form->setAction($this->url()); echo $this->form;