Zend Framework Quick Start. Create A Form

Translations of this material:

into Russian: 1.5 Zend Framework — Быстрый старт. Создание формы. Translation complete.
Submitted for translation by antdmi 07.02.2011 Published 7 years, 1 month ago.

Text

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:

    01. % zf create form Guestbook

    02. Creating a form at application/forms/Guestbook.php

    03. 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:

    01. // application/forms/Guestbook.php

    02.

    03. class Application_Form_Guestbook extends Zend_Form

    04. {

    05.     public function init()

    06.     {

    07.         // Set the method for the display form to POST

    08.         $this->setMethod('post');

    09.

    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:

    01. % zf create action sign Guestbook

    02. Creating an action named sign inside controller

    03.     at application/controllers/GuestbookController.php

    04. Updating project profile '.zfproject.xml'

    05. Creating a view script for the sign action method

    06.     at application/views/scripts/guestbook/sign.phtml

    07. 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:

    01. // application/controllers/GuestbookController.php

    02.

    03. class GuestbookController extends Zend_Controller_Action

    04. {

    05.     // snipping indexAction()...

    06.

    07.     public function signAction()

    08.     {

    09.         $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:

    01. <!-- application/views/scripts/guestbook/sign.phtml -->

    02.

    03. Please use the form below to sign our guestbook!

    04.

    05. <?php

    06. $this->form->setAction($this->url());

    07. echo $this->form;

    Note: Better Looking Forms

Pages: ← previous Ctrl next
1 2