ITeye博客

Zend Framework 1.10.4手册(ZF的快速启动五)

yangyz1984 · 2010-07-20 14:23 · zend framework · 编程语言 · PHP · Zend · 脚本 · Markdown

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

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

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

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

 

view plaincopy to clipboardprint?

  1. <?php
  2. class Application_Form_Guestbook extends Zend_Form
  3. {
  4. public function init()
  5. {
  6. // Set the method for the display form to POST
  7. $this->setMethod('post');
  8. // Add an email element
  9. $this->addElement('text', 'email', array(
  10. 'label'      => 'Your email address:',
  11. 'required'   => true,
  12. 'filters'    => array('StringTrim'),
  13. 'validators' => array(
  14. 'EmailAddress',
  15. )
  16. ));
  17. // Add the comment element
  18. $this->addElement('textarea', 'comment', array(
  19. 'label'      => 'Please Comment:',
  20. 'required'   => true,
  21. 'validators' => array(
  22. array('validator' => 'StringLength', 'options' => array(0, 20))
  23. )
  24. ));
  25. // Add a captcha
  26. $this->addElement('captcha', 'captcha', array(
  27. 'label'      => 'Please enter the 5 letters displayed below:',
  28. 'required'   => true,
  29. 'captcha'    => array(
  30. 'captcha' => 'Figlet',
  31. 'wordLen' => 5,
  32. 'timeout' => 300
  33. )
  34. ));
  35. // Add the submit button
  36. $this->addElement('submit', 'submit', array(
  37. 'ignore'   => true,
  38. 'label'    => 'Sign Guestbook',
  39. ));
  40. // And finally add some CSRF protection
  41. $this->addElement('hash', 'csrf', array(
  42. 'ignore' => true,
  43. ));
  44. }
  45. }

 

以上的表单定义了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 请求,我们需要验证提交的表单数据,并且,如果是合法的话,创建一个新的条目并保存它。这个逻辑看起来是这样的:

 

view plaincopy to clipboardprint?

  1. <?php
  2. class GuestbookController extends Zend_Controller_Action
  3. {
  4. // snipping indexAction()...
  5. public function signAction()
  6. {
  7. $request = $this->getRequest();
  8. $form    = new Application_Form_Guestbook();
  9. if ($this->getRequest()->isPost()) {
  10. if ($form->isValid($request->getPost())) {
  11. $comment = new Application_Model_Guestbook($form->getValues());
  12. $mapper  = new Application_Model_GuestbookMapper();
  13. $mapper->save($comment);
  14. return $this->_helper->redirector('index');
  15. }
  16. }
  17. $this->view->form = $form;
  18. }
  19. }

 

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

 

view plaincopy to clipboardprint?

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

 

注意:更好看的表单
。。。。。。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。