quickstart-create-form.xml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 19766 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="learning.quickstart.create-form">
  5. <title>Create A Form</title>
  6. <para>
  7. For our guestbook to be useful, we need a form for submitting new entries.
  8. </para>
  9. <para>
  10. Our first order of business is to create the actual form class. First, create the directory
  11. <filename>application/forms/</filename>. This directory will contain form classes for the
  12. application. Next, we'll create a form class in
  13. <filename>application/forms/Guestbook.php</filename>:
  14. </para>
  15. <programlisting language="php"><![CDATA[
  16. // application/forms/Guestbook.php
  17. class Default_Form_Guestbook extends Zend_Form
  18. {
  19. public function init()
  20. {
  21. // Set the method for the display form to POST
  22. $this->setMethod('post');
  23. // Add an email element
  24. $this->addElement('text', 'email', array(
  25. 'label' => 'Your email address:',
  26. 'required' => true,
  27. 'filters' => array('StringTrim'),
  28. 'validators' => array(
  29. 'EmailAddress',
  30. )
  31. ));
  32. // Add the comment element
  33. $this->addElement('textarea', 'comment', array(
  34. 'label' => 'Please Comment:',
  35. 'required' => true,
  36. 'validators' => array(
  37. array('validator' => 'StringLength', 'options' => array(0, 20))
  38. )
  39. ));
  40. // Add a captcha
  41. $this->addElement('captcha', 'captcha', array(
  42. 'label' => 'Please enter the 5 letters displayed below:',
  43. 'required' => true,
  44. 'captcha' => array(
  45. 'captcha' => 'Figlet',
  46. 'wordLen' => 5,
  47. 'timeout' => 300
  48. )
  49. ));
  50. // Add the submit button
  51. $this->addElement('submit', 'submit', array(
  52. 'ignore' => true,
  53. 'label' => 'Sign Guestbook',
  54. ));
  55. // And finally add some CSRF protection
  56. $this->addElement('hash', 'csrf', array(
  57. 'ignore' => true,
  58. ));
  59. }
  60. }
  61. ]]></programlisting>
  62. <para>
  63. The above form defines five elements: an email address field, a comment field, a CAPTCHA for
  64. preventing spam submissions, a submit button, and a CSRF protection token.
  65. </para>
  66. <para>
  67. Next, we will add a <methodname>signAction()</methodname> to our
  68. <classname>GuestbookController</classname> which will process the form upon submission. To
  69. create the action and related view script, execute the following:
  70. </para>
  71. <programlisting language="shell"><![CDATA[
  72. # Unix-like systems:
  73. % zf.sh create action sign guestbook
  74. # DOS/Windows:
  75. C:> zf.bat create action sign guestbook
  76. ]]></programlisting>
  77. <para>
  78. This will create a <methodname>signAction()</methodname> method in our controller, as well
  79. as the appropriate view script.
  80. </para>
  81. <para>
  82. Let's add some logic into our guestbook controller's sign action. We need to first check if
  83. we're getting a POST or a GET request; in the latter case, we'll simply display the form.
  84. However, if we get a POST request, we'll want to validate the posted data against our form,
  85. and, if valid, create a new entry and save it. The logic might look like this:
  86. </para>
  87. <programlisting language="php"><![CDATA[
  88. // application/controllers/GuestbookController.php
  89. class GuestbookController extends Zend_Controller_Action
  90. {
  91. // snipping indexAction()...
  92. public function signAction()
  93. {
  94. $request = $this->getRequest();
  95. $form = new Default_Form_Guestbook();
  96. if ($this->getRequest()->isPost()) {
  97. if ($form->isValid($request->getPost())) {
  98. $model = new Default_Model_Guestbook($form->getValues());
  99. $model->save();
  100. return $this->_helper->redirector('index');
  101. }
  102. }
  103. $this->view->form = $form;
  104. }
  105. }
  106. ]]></programlisting>
  107. <para>
  108. Of course, we also need to edit the view script; edit <filename>application/views/scripts/guestbook/sign.phtml</filename> to read:
  109. </para>
  110. <programlisting language="php"><![CDATA[
  111. <!-- application/views/scripts/guestbook/sign.phtml -->
  112. Please use the form below to sign our guestbook!
  113. <?php
  114. $this->form->setAction($this->url());
  115. echo $this->form;
  116. ]]></programlisting>
  117. <note>
  118. <title>Better Looking Forms</title>
  119. <para>
  120. No one will be waxing poetic about the beauty of this form anytime soon. No matter -
  121. form appearance is fully customizable! See the <link
  122. linkend="zend.form.decorators">decorators section in the reference guide</link>
  123. for details.
  124. </para>
  125. <para>
  126. Additionally, you may be interested in <ulink
  127. url="http://weierophinney.net/matthew/plugin/tag/decorators">this series of posts on
  128. decorators</ulink>.
  129. </para>
  130. </note>
  131. <note>
  132. <title>Checkpoint</title>
  133. <para>
  134. Now browse to "http://localhost/guestbook/sign". You should see the following in your
  135. browser:
  136. </para>
  137. <para>
  138. <inlinegraphic width="421" scale="100" align="center" valign="middle"
  139. fileref="figures/learning.quickstart.create-form.png" format="PNG" />
  140. </para>
  141. </note>
  142. </sect1>