Zend_Form component in the ZendFramework tutorial to implement form submission and display error messages. zendzend_form_PHP tutorial

Source: Internet
Author: User
Tags valid email address
The Zend_Form component in the ZendFramework tutorial implements the method of submitting a form and displaying an error message, zendzend_form. In the ZendFramework tutorial, use the Zend_Form component to submit a form and display an error message, zendzend_form the Zend_Form component used in the ZendFramework tutorial is used to implement form submission and Zend Framework tutorial. the Zend_Form component implements form submission and displays error messages. zendzend_form

This article describes how to implement form submission and display error messages by using the Zend_Form component in the Zend Framework tutorial. We will share this with you for your reference. The details are as follows:

At the same time, the company needs to develop a group function .. I also want to use Zend_Form to create a group. the main idea is that Zend_Form can be used when writing Form. implement the server-side verification function .. this saves us the trouble of re-verifying the data when it is submitted to the database .. so. I read this manual .. relevant instructions are found in the Zend Framework manual... the simplest way to use it is to write a ready-made Action in the Controller .. in this controller, you can directly use this Action... the code can be as follows:

<? Phppublic function formAction () {$ form = new Zend_Form (); $ form-> setName ('Group'); $ title = new Zend_Form_Element_Select ('title '); $ title-> setLabel ('gender')-> setMultiOptions (array ('Mr '=> 'Mr', 'Mrs '=> 'Mrs ')) -> setRequired (true)-> addValidator ('notempty ', true); $ yourName = new Zend_Form_Element_Text ('firstname'); $ yourName-> setLabel ('name ') -> setRequired (true)-> addValidator ('notempty ', true); $ email = new Ze Nd_Form_Element_Text ('email '); $ email-> setLabel ('email address')-> addFilter ('stringtolower')-> setRequired (false) -> addValidator ('notempty '); $ submit = new Zend_Form_Element_Submit ('submit'); $ submit-> setLabel ('Group '); $ form-> addElements (array ($ title, $ yourName, $ email, $ submit);}?>

Of course .. I can also write this Form into a class... stored in a common directory of forms. this makes it easy for us to manage all our Form forms .. my implementation method is to put it in the same directory as the Controller... this makes it easy to manage .. of course different friends .. different ideas... another way .. you can write it as View Helper... this method is easy to implement .. I don't want to write more here... zend Framework is easy to implement... as long as you want... no matter how you complete your task, it is okay .. here I will not talk about anything else... I just want to talk about how to enable Zend_Form to implement the prompt information function in Chinese... I have two methods here ..

First, the stupid way is: If your website is not made into a multi-language website .. at the same time, if your Zend Framework version is not changed frequently... you can find the source code of the relevant prompt information... prompt for changing to Chinese.

This stupid method... there is really no way...

Second, I am also on the English site... A better method is to rewrite the prompt information. replace it with the language we want... so... even if we change the language .. or change the Zend Framework version ..

The impact on us is not very big... we only need to change the Form of our Form .. the code for this method is as follows (I only wrote Email prompts here .. do not write more ):

<? Php $ email = new Zend_Form_Element_Text ('email '); $ email-> setLabel ('email address')-> addFilter ('stringtolower')-> setRequired (false) -> addValidator ('notempty ')-> addValidator ('emailaddress', true, array ('messages '=> array ('emailaddressinvalid' =>' this is not an available email! ', 'Emailaddressinvalidhostname' =>' this is not a valid host name! ', 'Emailaddressinvalidmxrecord' => 'is not a valid email address! ', 'Emailaddressdotatom' => 'is not a valid email address! ', 'Emailaddressquotedstring' =>' is not a valid email address! ', 'Emailaddressinvalidlocalpart' =>' is not a valid email address! ',);?>

Here .. the Zend_Form component also has an important function .. zend_Form_Decorator .. the manual is called a decoration device, which means you can write the decoration device you want yourself .. for example .. you want to include your Form in Table .. how can we implement it? This time .. we need to use the decorators such as HtmlTag and Label to achieve the functions we want... here is a very important concept .. if you are interested, you can go and have a look... because if you want to use the Zend_Form component .. no decorator. it will be difficult to use it .. so you must have this thing .. to create the form function you want .. last .. it is a small use of a decoration device.

I just implemented a small function... the following code:

<? Php $ email = new Zend_Form_Element_Text ('email '); $ email-> setLabel ('email address')-> addFilter ('stringtolower')-> setRequired (false) // use the decorator to add the td tag-> addDecorator ('htmltag', array ('tag' => 'TD ')-> addDecorator ('label ', array ('tag' => 'TD ') // reuse the HtmlTag modifier to add the tr tag-> addDecorator (array ('footr' => 'htmltag '), array ('tag' => 'tr')-> addValidator ('notempty ');?>

Haha .... the general application is like this... last .. is to verify the submitted data... how to verify the data submitted by the user .... I will not talk about it here... OK...

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.