Parsing how to create a form form _php tutorial under Zend Farmework

Source: Internet
Author: User
Tags autoloader
1. First let us set up our program, so that Zend can automatically load the method, do not need us to manually load
Copy CodeThe code is as follows:
Require_once ' zend/loader/autoloader.php '//load Auto Load Class
$loader = Zend_loader_autoloader::getinstance ();//Automatic instantiation
$loader->registernamespace (' application_ ');//Register namespace (only system default, and registered only can be automatically loaded)
$loader->registernamespace (Array (' Foo_ ', ' bar_ ')); How to register multiple namespaces
$loader->setfallbackautoloader (TRUE); A method of increasing consumption, no namespace required, loading all classes directly (not recommended)

Then please note that your include directory is already included, your own directory that needs to be loaded
Copy 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, which facilitates the loading of programs

2. Confirm the list of form below
Build a Guestbook.phps under the application/forms/
As the class file for our form, as follows:
Copy CodeThe 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 ');//Set Submission method

Add an email element
$this->addelement (' text ', ' email ', array (//Definition of the original type, noun, and some other information
' 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,
));
}
}

Then add a route control file
applictaion/controller/guestbookcontroller.php
Copy CodeThe code is as follows:
Class Guestbookcontroller extends Zend_controller_action
{
Snipping Indexaction () ...
Public Function Signaction ()
{
$request = $this->getrequest ();//Get received information
Include_once (".. /application/forms/guestbook.php "); Manually loading classes, only if they cannot be loaded automatically.
$form = new application_form_guestbook;//instantiation of this method

if ($this->getrequest ()->ispost ()) {//If it is the result of post delivery
if ($form->isvalid ($request->getpost ())) {//Determine if delivery 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;//assigns the form to an attempt to
}
}

Finally, add a simple sign view file:
Address: application/views/scripts/guestbook/sgin.php
Copy CodeThe code is as follows:
Please use the form below to sign our guestbook!
$this->form->setaction ($this->url ());
Echo $this->form;

http://www.bkjia.com/PHPjc/327948.html www.bkjia.com true http://www.bkjia.com/PHPjc/327948.html techarticle 1. First let us set up our program, so that Zend can automatically load the method, do not need us to manually load the copy code code as follows: require_once ' ZEND/LOADER/AUTOLOADER.P ...

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