Create A Form

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;

NoteBetter Looking Forms
No one will be waxing poetic about the beauty of this form anytime soon. No matter - form appearance is fully customizable! See thedecorators section in the reference guide for details. 
Additionally, you may be interested in our tutorial on form decorators

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

learning.quickstart.create-form.png
 

Congratulations!

You have now built a very simple application using some of the most commonly used Zend Framework components. Zend Framework makes many components available to you which address most common requirements in web applications, including web services, search,PDF reading and writing, authentication, authorization, and much more. The Reference Guide is a great place to find out more about the components you've used in this QuickStart as well as other components. We hope you find Zend Framework useful and - more importantly - fun!

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值