Zend Framework 1.10.1 快速入门之五:创建一个表单

为了使我们的留言本有用,我们需要一个可以提交新条目的表单。

我们第一件事是要创建事实表单的类。为了创建空的表单类,执行:

      % zf create form Guestbook
      Creating a form at application/forms/Guestbook.php
      Updating project profile '.zfproject.xml'

这将创建 application/forms/ 目录,同时带有 Guestbook.php 这个类文件。打开文件,更新内容如下:

<?php class Application_Form_Guestbook extends Zend_Form {     public function init()     {         // Set the method for the display form to POST         $this->setMethod('post');         // Add an email element         $this->addElement('text', 'email', array(             'label'      => 'Your email address:',             'required'   => true,             'filters'    => array('StringTrim'),             'validators' => array(                 'EmailAddress',             )         ));         // Add the comment element         $this->addElement('textarea', 'comment', array(             'label'      => 'Please Comment:',             'required'   => true,             'validators' => array(                 array('validator' => 'StringLength', 'options' => array(0, 20))                 )         ));         // Add a captcha         $this->addElement('captcha', 'captcha', array(             'label'      => 'Please enter the 5 letters displayed below:',             'required'   => true,             'captcha'    => array(                 'captcha' => 'Figlet',                 'wordLen' => 5,                 'timeout' => 300             )         ));         // Add the submit button         $this->addElement('submit', 'submit', array(             'ignore'   => true,             'label'    => 'Sign Guestbook',         ));         // And finally add some CSRF protection         $this->addElement('hash', 'csrf', array(             'ignore' => true,         ));     } }

以上的表单定义了5个元素:一个邮件地址字段,一个留言字段,一个 CAPTCHA 用来防止拉圾提交,一个提交按钮,和一个 CSRF 保护口令。

下一步,我们需要添加一个 signAction() 到我们的 GuestbookController,它将接受提交,处理表单。为了创建行为和相关的视图脚本,执行以下命令:

      % zf create action sign Guestbook
      Creating an action named sign inside controller
          at application/controllers/GuestbookController.php
      Updating project profile '.zfproject.xml'
      Creating a view script for the sign action method
          at application/views/scripts/guestbook/sign.phtml
      Updating project profile '.zfproject.xml'

正如你所看到的,这将创建一个 signAction() 方法到我们控制器中,同时还有相对应的视图脚本。

让我们向我们的 guestbook 控制器的 sign 行为添加一些逻辑。我们需要首先检查我们收到的是一个 POST 还是一个 GET 请求;在后面一种情况下,我们只需简单的显示表单。然而,如果我们得到的是一个 POST 请求,我们需要验证提交的表单数据,并且,如果是合法的话,创建一个新的条目并保存它。这个逻辑看起来是这样的:

<?php class GuestbookController extends Zend_Controller_Action {     // snipping indexAction()...         public function signAction()     {         $request = $this->getRequest();         $form    = new Application_Form_Guestbook();         if ($this->getRequest()->isPost()) {             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;     } }

当然,我们也要编辑视图脚本;编辑 application/views/scripts/guestbook/sign.phtml 如下:

<!-- application/views/scripts/guestbook/sign.phtml --> Please use the form below to sign our guestbook! <?php $this->form->setAction($this->url()); echo $this->form;

注意:更好看的表单
。。。。。。
注意:检查
现在浏览 http://localhost/guestbook/sign。你应该在你的浏览器中能看到:

你可能感兴趣的:(脚本,application,action,email,Zend,CSRF)