quickstart-create-form.xml 6.0 KB

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