This article is about how to create a form under the Zend Farmework a detailed analysis of the method, the need for friends under the reference
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 registration can be automatically loaded)
$loader->registernamespace (Array (' Foo_ ', ' bar_ ')); Registration method 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 the way to submit
//Add an email element
$this->addelement (' text ', ' email ', array (//) The type of the original, nouns, and some other information about the definition
' 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 you cannot load them automatically, you need
$form = new application_form_guestbook;//Instantiate this method
if ($this->getrequest ()->ispost ()) {//If the result of a post pass
if ($form->isvalid ($request->getpost ())) {//To determine whether 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:
the form below to sign our guestbook!
<?php
$this->form->setaction ($this->url ());
Echo $this->form;