Web Frameworks - Workbook - Week 05

From mi-linux
Jump to navigationJump to search

Main Page >> Web Frameworks >> Workbook >> Workshop - week 05

Create a Form

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

Step 1 - create the form class

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:

<?php
// application/forms/Guestbook.php

class Default_Form_Guestbook extends Zend_Form
{
    public function init()
    {
        // Set the method for the display form to POST
        $this->setMethod('post');

        // Add an email element
        $this->addElement('text', 'email', array(
            'label'      => 'Your email address:',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array(
                'EmailAddress',
            )
        ));

        // Add the comment element
        $this->addElement('textarea', 'comment', array(
            'label'      => 'Please Comment:',
            'required'   => true,
            'validators' => array(
                array('validator' => 'StringLength', 'options' => array(0, 20))
                )
        ));

        // Add a captcha
        $this->addElement('captcha', 'captcha', array(
            'label'      => 'Please enter the 5 letters displayed below:',
            'required'   => true,
            'captcha'    => array(
                'captcha' => 'Figlet', 
                'wordLen' => 5, 
                'timeout' => 300
            )
        ));

        // Add the submit button
        $this->addElement('submit', 'submit', array(
            'ignore'   => true,
            'label'    => 'Sign Guestbook',
        ));

        // And finally add some CSRF protection
        $this->addElement('hash', 'csrf', array(
            'ignore' => true,
        ));
    }
}

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.

Step 2 - Create new action and view

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:

% zf.sh create action sign guestbook

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

Step 3 - Edit our new action

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:

<?php
// application/controllers/GuestbookController.php

class GuestbookController extends Zend_Controller_Action
{
    // snipping indexAction()...

    public function signAction()
    {
        $request = $this->getRequest();
        $form    = new Default_Form_Guestbook();

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())) {
                $model = new Default_Model_Guestbook($form->getValues());
                $model->save();
                return $this->_helper->redirector('index');
            }
        }
        
        $this->view->form = $form;
    }
}

Step 4 - Edit our new view

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

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

Please use the form below to sign our guestbook!

<?php 
$this->form->setAction($this->url());
echo $this->form;

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.

Bg-checkpoint.png Checkpoint

Browse to your project: http://mi-linux.wlv.ac.uk/~0123456/quickstart/public/guestbook

Click on the "Sign Our Guestbook", fill the form, and press the "Sign Guestbook" button... all done!

Zend 04.jpg