Parse how to create a FORM under zendFarmework

Source: Internet
Author: User
This article provides a detailed analysis of how to create a FORM under zendFarmework. For more information, see

This article provides a detailed analysis of how to create a FORM under zend Farmework. For more information, see

1. First, let's set up our program so that Zend can automatically load the method without manual loading.

The Code is as follows:


Require_once 'zend/Loader/Autoloader. php' // load the automatic loading class
$ Loader = Zend_Loader_Autoloader: getInstance (); // automatically instantiate
$ Loader-> registerNamespace ('application _ '); // register the namespace (only the default and registered namespaces can be automatically loaded)
$ Loader-> registerNamespace (array ('foo _ ', 'bar _'); // register multiple namespaces
$ Loader-> setFallbackAutoloader (true); // a method that increases consumption. It does not require namespaces, virtual hosts, and directly loads all classes (not recommended)


Then pay attention to whether your include directory already contains your own directory to be loaded

The Code is as follows:


Set_include_path (implode (PATH_SEPARATOR, array (
Realpath (APPLICATION_PATH. '/../library '),
Realpath (APPLICATION_PATH. '/forms /'),
Get_include_path (),
)));
// Here we include our forms directory to facilitate program loading


2. Check the form directory.
Create a Guestbook. phps under application/forms/
As a form class file, it is as follows:

The Code is as follows:


Class Application_Form_Guestbook extends Zend_Form
{
Public function init ()
{
// Set the method for the display form to POST
$ This-> setMethod ('post'); // you can specify the submission method.

// Add an email element
$ This-> addElement ('text', 'email ', array (// original type, term, Hong Kong Vm, and definition of some other information
'Label' => 'your email address :',
'Requestred' => true,
'Filters '=> array ('stringtrim '),
'Validatator' => array (
'Emailaddress ',
)
));

// Add the comment element
$ This-> addElement ('textea ', 'comment', array (
'Label' => 'Please Comment :',
'Requestred' => true,
'Validatator' => array (
Array ('validator' => 'stringlength', 'options' => array (0, 20 ))
)
));

// Add a captcha
$ This-> addElement ('captcha ', 'captcha', array (
'Label' => 'Please enter the 5 letters displayed below :',
'Requestred' => true,
'Captcha '=> array (
'Captcha '=> 'figlet ',
'Wordlen' => 5,
'Timeout' = & gt; 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,
));
}
}


Then add a route control file
Applictaion/controller/GuestbookController. php

The Code is as follows:


Class GuestbookController extends Zend_Controller_Action
{
// Snipping indexAction ()...
Public function signAction ()
{
$ Request = $ this-> getRequest (); // obtain the received information
// Include_once ("../application/forms/Guestbook. php"); manually load the class. Only when the class cannot be automatically loaded will the website space be required
$ Form = new Application_Form_Guestbook; // instantiate this method

If ($ this-> getRequest ()-> isPost () {// if the result is passed by POST
If ($ form-> isValid ($ request-> getPost () {// determines whether the pass is valid
$ Comment = new Application_Model_Guestbook ($ form-> getValues ());
$ Mapper = new Application_Model_GuestbookMapper ();
$ Mapper-> save ($ comment );
Return $ this-> _ helper-> redirector ('index ');
}
}

$ This-> view-> form = $ form; // assign a value to an attempt
}
}


Add a simple sign View File:
Address: application/views/scripts/guestbook/sgin. php

The Code is as follows:


Please use the form below to sign our guestbook!
$ This-> form-> setAction ($ this-> url ());
Echo $ this-> form;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.