---
title: "Zend Framework 1.10.4手册(ZF的快速启动五)"
author: "yangyz1984"
date: "2010-07-20T14:23:46Z"
tags: ["PHP", "Zend", "脚本"]
categories: ["编程语言"]
slug: "zend-framework-1-10-4-zf"
draft: false
lastmod: "2010-07-20T14:43:08Z"
id: 716929
url: "https://yyzstudy.iteye.com/blog/716929"
---

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

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

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

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

 

[view plain](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)[copy to clipboard](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)[print](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)[?](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)

01. &lt;?php
02. **class** Application\_Form\_Guestbook **extends** Zend\_Form
03. {
04. **public** **function** init()
05. {
06. // Set the method for the display form to POST
07. $this-&gt;setMethod('post');
08. // Add an email element
09. $this-&gt;addElement('text', 'email', **array**(
10. 'label'      =&gt; 'Your email address:',
11. 'required'   =&gt; true,
12. 'filters'    =&gt; **array**('StringTrim'),
13. 'validators' =&gt; **array**(
14. 'EmailAddress',
15. )
16. ));
17. // Add the comment element
18. $this-&gt;addElement('textarea', 'comment', **array**(
19. 'label'      =&gt; 'Please Comment:',
20. 'required'   =&gt; true,
21. 'validators' =&gt; **array**(
22. **array**('validator' =&gt; 'StringLength', 'options' =&gt; **array**(0, 20))
23. )
24. ));
25. // Add a captcha
26. $this-&gt;addElement('captcha', 'captcha', **array**(
27. 'label'      =&gt; 'Please enter the 5 letters displayed below:',
28. 'required'   =&gt; true,
29. 'captcha'    =&gt; **array**(
30. 'captcha' =&gt; 'Figlet',
31. 'wordLen' =&gt; 5,
32. 'timeout' =&gt; 300
33. )
34. ));
35. // Add the submit button
36. $this-&gt;addElement('submit', 'submit', **array**(
37. 'ignore'   =&gt; true,
38. 'label'    =&gt; 'Sign Guestbook',
39. ));
40. // And finally add some CSRF protection
41. $this-&gt;addElement('hash', 'csrf', **array**(
42. 'ignore' =&gt; 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 plain](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)[copy to clipboard](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)[print](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)[?](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)

01. &lt;?php
02. **class** GuestbookController **extends** Zend\_Controller\_Action
03. {
04. // snipping indexAction()...
05. **public** **function** signAction()
06. {
07. $request = $this-&gt;getRequest();
08. $form    = **new** Application\_Form\_Guestbook();
09. **if** ($this-&gt;getRequest()-&gt;isPost()) {
10. **if** ($form-&gt;isValid($request-&gt;getPost())) {
11. $comment = **new** Application\_Model\_Guestbook($form-&gt;getValues());
12. $mapper  = **new** Application\_Model\_GuestbookMapper();
13. $mapper-&gt;save($comment);
14. **return** $this-&gt;\_helper-&gt;redirector('index');
15. }
16. }
17. $this-&gt;view-&gt;form = $form;
18. }
19. }

 

当然，我们也要编辑视图脚本；编辑 application/views/scripts/guestbook/sign.phtml 如下：

 

[view plain](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)[copy to clipboard](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)[print](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)[?](http://blog.csdn.net/klinghr/archive/2010/03/23/5406518.aspx)

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

 

> 注意：更好看的表单  
> 。。。。。。