quickstart-create-form.xml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 CAPTCHA for
  63. preventing spam submissions, a submit button, and a CSRF protection token.
  64. </para>
  65. <para>
  66. Next, we will add a <methodname>signAction()</methodname> to our
  67. <classname>GuestbookController</classname> which will process the form upon submission. To
  68. create the action and related view script, execute the following:
  69. </para>
  70. <programlisting language="shell"><![CDATA[
  71. # Unix-like systems:
  72. % zf.sh create action sign guestbook
  73. # DOS/Windows:
  74. C:> zf.bat create action sign guestbook
  75. ]]></programlisting>
  76. <para>
  77. This will create a <methodname>signAction()</methodname> method in our controller, as well
  78. as the appropriate view script.
  79. </para>
  80. <para>
  81. Let's add some logic into our guestbook controller's sign action. We need to first check if
  82. we're getting a POST or a GET request; in the latter case, we'll simply display the form.
  83. However, if we get a POST request, we'll want to validate the posted data against our form,
  84. and, if valid, create a new entry and save it. The logic might look like this:
  85. </para>
  86. <programlisting language="php"><![CDATA[
  87. // application/controllers/GuestbookController.php
  88. class GuestbookController extends Zend_Controller_Action
  89. {
  90. // snipping indexAction()...
  91. public function signAction()
  92. {
  93. $request = $this->getRequest();
  94. $form = new Default_Form_Guestbook();
  95. if ($this->getRequest()->isPost()) {
  96. if ($form->isValid($request->getPost())) {
  97. $model = new Default_Model_Guestbook($form->getValues());
  98. $model->save();
  99. return $this->_helper->redirector('index');
  100. }
  101. }
  102. $this->view->form = $form;
  103. }
  104. }
  105. ]]></programlisting>
  106. <para>
  107. Of course, we also need to edit the view script; edit <filename>application/views/scripts/guestbook/sign.phtml</filename> to read:
  108. </para>
  109. <programlisting language="php"><![CDATA[
  110. <!-- application/views/scripts/guestbook/sign.phtml -->
  111. Please use the form below to sign our guestbook!
  112. <?php
  113. $this->form->setAction($this->url());
  114. echo $this->form;
  115. ]]></programlisting>
  116. <note>
  117. <title>Better Looking Forms</title>
  118. <para>
  119. No one will be waxing poetic about the beauty of this form anytime soon. No matter -
  120. form appearance is fully customizable! See the <link
  121. linkend="zend.form.decorators">decorators section in the reference guide</link>
  122. for details.
  123. </para>
  124. <para>
  125. Additionally, you may be interested in <ulink
  126. url="http://weierophinney.net/matthew/plugin/tag/decorators">this series of posts on
  127. decorators</ulink>.
  128. </para>
  129. </note>
  130. <note>
  131. <title>Checkpoint</title>
  132. <para>
  133. Now browse to "http://localhost/guestbook/sign". You should see the following in your
  134. browser:
  135. </para>
  136. <para>
  137. <inlinegraphic width="421" scale="100" align="center" valign="middle"
  138. fileref="figures/learning.quickstart.create-form.png" format="PNG" />
  139. </para>
  140. </note>
  141. </sect1>