Documentation

Create A Form - Zend Framework Quick Start

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.  
  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. % 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.  
  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.                 $comment = new Application_Model_Guestbook($form->getValues());
  15.                 $mapper  = new Application_Model_GuestbookMapper();
  16.                 $mapper->save($comment);
  17.                 return $this->_helper->redirector('index');
  18.             }
  19.         }
  20.  
  21.         $this->view->form = $form;
  22.     }
  23. }

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 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

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts