Parsing how to create a form under the Zend Farmework

Source: Internet
Author: User
Tags zend
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;

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.