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. To create the empty form class, execute:

  1. % zf create form Guestbook
  2. Creating a form at application/forms/Guestbook.php
  3. Updating project profile '.zfproject.xml'

This will create the directory application/forms/ with the classfile Guestbook.php. Open that file and update it so it reads as follows:

  1. // application/forms/Guestbook.php
  2. // Set the method for the display form to POST
  3.         $this->setMethod('post');
  4.  
  5.         // Add an email element
  6.         $this->addElement('text', 'email''label'      => 'Your email address:',
  7.             'required''filters''StringTrim'),
  8.             'validators''EmailAddress',
  9.             )
  10.         ));
  11.  
  12.         // Add the comment element
  13.         $this->addElement('textarea', 'comment''label'      => 'Please Comment:',
  14.             'required''validators''validator' => 'StringLength', 'options'// Add a captcha
  15.         $this->addElement('captcha', 'captcha''label'      => 'Please enter the 5 letters displayed below:',
  16.             'required''captcha''captcha' => 'Figlet',
  17.                 'wordLen' => 5,
  18.                 'timeout' => 300
  19.             )
  20.         ));
  21.  
  22.         // Add the submit button
  23.         $this->addElement('submit', 'submit''ignore''label'    => 'Sign Guestbook',
  24.         ));
  25.  
  26.         // And finally add some CSRF protection
  27.         $this->addElement('hash', 'csrf''ignore'

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. % zf create action sign Guestbook
  2. Creating an action named sign inside controller
  3.     at application/controllers/GuestbookController.php
  4. Updating project profile '.zfproject.xml'
  5. Creating a view script for the sign action method
  6.     at application/views/scripts/guestbook/sign.phtml
  7. Updating project profile '.zfproject.xml'

As you can see from the output, 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. // snipping indexAction()...
  3. 'index'

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

  1.  

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 our tutorial on form 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