Create a Model and Database Table

Create A Form

For our guestbook to be useful, we need a form for submitting new entries.

Our first order of business is to create the actual form class. First, create the directory application/forms/. This directory will contain form classes for the application. Next, we'll create a form class in application/forms/Guestbook.php:

  1. // application/forms/Guestbook.php
  2.  
  3. class Application_Form_Guestbook extends Zend_Form
  4. {
  5.     public function init()
  6.     {
  7.         // Set the method for the display form to POST
  8.         $this->setMethod('post');
  9.  
  10.         // Add an email element
  11.         $this->addElement('text', 'email', array(
  12.             'label'      => 'Your email address:',
  13.             'required'   => true,
  14.             'filters'    => array('StringTrim'),
  15.             'validators' => array(
  16.                 'EmailAddress',
  17.             )
  18.         ));
  19.  
  20.         // Add the comment element
  21.         $this->addElement('textarea', 'comment', array(
  22.             'label'      => 'Please Comment:',
  23.             'required'   => true,
  24.             'validators' => array(
  25.                 array('validator' => 'StringLength', 'options' => array(0, 20))
  26.                 )
  27.         ));
  28.  
  29.         // Add a captcha
  30.         $this->addElement('captcha', 'captcha', array(
  31.             'label'      => 'Please enter the 5 letters displayed below:',
  32.             'required'   => true,
  33.             'captcha'    => array(
  34.                 'captcha' => 'Figlet',
  35.                 'wordLen' => 5,
  36.                 'timeout' => 300
  37.             )
  38.         ));
  39.  
  40.         // Add the submit button
  41.         $this->addElement('submit', 'submit', array(
  42.             'ignore'   => true,
  43.             'label'    => 'Sign Guestbook',
  44.         ));
  45.  
  46.         // And finally add some CSRF protection
  47.         $this->addElement('hash', 'csrf', array(
  48.             'ignore' => true,
  49.         ));
  50.     }
  51. }

The above form defines five elements: an email address field, a comment field, a CAPTCHA for preventing spam submissions, a submit button, and a CSRF protection token.

Next, we will add a signAction() to our GuestbookController which will process the form upon submission. To create the action and related view script, execute the following:

  1. # Unix-like systems:
  2. % zf.sh create action sign guestbook
  3.  
  4. # DOS/Windows:
  5. C:> zf.bat create action sign guestbook

This will create a signAction() method in our controller, as well as the appropriate view script.

Let's add some logic into our guestbook controller's sign action. We need to first check if we're getting a POST or a GET request; in the latter case, we'll simply display the form. However, if we get a POST request, we'll want to validate the posted data against our form, and, if valid, create a new entry and save it. The logic might look like this:

  1. // application/controllers/GuestbookController.php
  2.  
  3. class GuestbookController extends Zend_Controller_Action
  4. {
  5.     // snipping indexAction()...
  6.  
  7.     public function signAction()
  8.     {
  9.         $request = $this->getRequest();
  10.         $form    = new Application_Form_Guestbook();
  11.  
  12.         if ($this->getRequest()->isPost()) {
  13.             if ($form->isValid($request->getPost())) {
  14.                 $model = new Application_Model_Guestbook($form->getValues());
  15.                 $model->save();
  16.                 return $this->_helper->redirector('index');
  17.             }
  18.         }
  19.  
  20.         $this->view->form = $form;
  21.     }
  22. }

Of course, we also need to edit the view script; edit application/views/scripts/guestbook/sign.phtml to read:

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

Note: Better Looking Forms
No one will be waxing poetic about the beauty of this form anytime soon. No matter - form appearance is fully customizable! See the decorators section in the reference guide for details.
Additionally, you may be interested in » this series of posts on decorators.

Note: Checkpoint
Now browse to "http://localhost/guestbook/sign". You should see the following in your browser:

learning.quickstart.create-form.png


Create a Model and Database Table