Parsing how to create a form _php technique under Zend farmework

Source: Internet
Author: User
Tags zend
1. First let us set up our program, so that Zend can automatically load methods, do not require us to manually load
Copy Code code 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 can be automatically loaded)
$loader->registernamespace (Array (' Foo_ ', ' bar_ ')); Registration Methods for multiple namespaces
$loader->setfallbackautoloader (TRUE); A method of increasing consumption without namespaces, loading all classes directly (not recommended)

Then please note that your inclusion directory is already included, your own directory to be loaded
Copy Code code 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 the loading of programs

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

Add an email element
$this->addelement (' text ', ' email ', array (//) The type of the original, nouns, and some other information defined
' 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 Routing control file
applictaion/controller/guestbookcontroller.php
Copy Code code as follows:

<?php
Class Guestbookcontroller extends Zend_controller_action
{
Snipping Indexaction () ...
Public Function Signaction ()
{
$request = $this->getrequest ();//Get received information
Include_once ("..  /application/forms/guestbook.php "); Load classes manually, only if they cannot be loaded automatically.
$form = new application_form_guestbook;//instantiation of this method

if ($this->getrequest ()->ispost ()) {//If the result of a post pass
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;//Assign the form to an attempt to
}
}

Finally, add a simple sign view file:
Address: application/views/scripts/guestbook/sgin.php
Copy Code code 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.