quickstart-create-form.xml 5.5 KB

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