Introducción a Zend Framework

 Aprendiendo Zend Framework


 Referencia de Zend Framework

  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Traducción al 26.9% - Actualizado el 2011-11-16 - Revisión 24249 - Versión ZF 1.11.x

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

    zf create form Guestbook
    Creating a form at application
    Updating project profile 

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

    // application/forms/Guestbook.php

    class Application_Form_Guestbook extends Zend_Form
        public function 
    // Set the method for the display form to POST

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

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

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

    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 create action sign Guestbook
    Creating an action named sign inside controller
        at application
    Updating project profile 
    Creating a view script for the sign action method
        at application
    Updating project profile 

    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:

    // application/controllers/GuestbookController.php

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

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

            if (
    $this->getRequest()->isPost()) {
                if (
    $form->isValid($request->getPost())) {
    $comment = new Application_Model_Guestbook($form->getValues());
    $mapper  = new Application_Model_GuestbookMapper();

    $this->view->form $form;

    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!

    [Nota] 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.

    [Nota] Checkpoint

    Now browse to "http://localhost/guestbook/sign". You should see the following in your browser:

    digg delicious meneame google twitter technorati facebook