Zend Framework, creating a contact form

Creating a contact form is quite straightforward in the Zend Framework.

zf create form Contact

zf create controller Form

zf create view Form

Specify the form in the Application_Form_Contact class in application/forms/Contact.php

class Application_Form_Contact extends Zend_Form
{

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

        // Add a name element
        $this->addElement('text', 'name', array(
            'label' => 'Your name:',
            'required' => true,
            'filters' => array('StringTrim'),
            'validators' => array(
                'NotEmpty',
            )
        ));

        $this->addElement('text', 'phone', array(
            'label' => 'Your phone:',
            'required' => false,
            'filters' => array('StringTrim'),

        ));

        // 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,
        ));
    }

}

Write an action in application/controllers/FormController.php

public function contactAction()
    {
        $request = $this->getRequest();
        $form = new Application_Form_Contact();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())) {
                echo 'OK';
            }
        }

        $this->view->form = $form;
    }

Update the routes so that /contact will get the contact form in /Bootstrap.php in the _initRoutes function. Note: the routes are matched in reverse order so the most generic one has to be first.

$frontController = Zend_Controller_Front::getInstance();
        $router = $frontController->getRouter();

        $contact = new Zend_Controller_Router_Route('contact',array('controller'=>'Form',
 'action'=>'contact'));

        $route_content = new Zend_Controller_Router_Route_Regex(
            '(\w*)',
            array('controller' => 'content',
                'action' => 'index' ),
            array(1 => 'url')
        );

        try{
            $router->addRoute('content', $route_content);
            $router->addRoute('form', $contact);
        }catch(Exception $e) {
            print($e->getTraceAsString());
        }

All fine, this is what I get:

Comments are closed.