Parse how to create a FORM under zend Farmework

Source: Internet
Author: User

1. First, let's set up our program so that Zend can automatically load the method without manual loading.
Copy codeThe 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 and directly loads all classes without namespaces (not recommended)

Then pay attention to whether your include directory already contains your own directory to be loadedCopy codeThe 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:Copy codeThe Code is as follows: <? Php
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, noun, 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. phpCopy codeThe Code is as follows: <? Php
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 loaded class, required only when it cannot be loaded automatically
$ 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. phpCopy codeThe Code is as follows: Please use the form below to sign our guestbook!
<? Php
$ 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.