Zend Framework Tutorial Zend_form components implement form submission and display error prompts _php instance

Source: Internet
Author: User

This example describes the Zend Framework Tutorial's Zend_form component implementation form submission and displaying error prompts. Share to everyone for your reference, specific as follows:

At the same time, the company also developed a group function. I would also like to use Zend_form to implement the function of creating a group. The main or fancy zend_form can be written when the form. Implement server-side validation capabilities. It saves us from validating the data once we submit it to the database. So I took a look at this manual. The relevant usage instructions were found through the Zend Framework manual ... The easiest way to use this is to write a ready-made action in the controller (Controller) so that ... You can use this action directly in this controller ... The code can be as follows:

 <?php public Function formaction () {$form =new zend_form (); $form->setname (' group '
);
$title = new Zend_form_element_select (' title '); $title->setlabel (' Gender ')->setmultioptions (' Mr ' => ' Mr ', ' Mrs ' => ')->setrequired (True)->
Addvalidator (' Notempty ', true);
$yourName = new Zend_form_element_text (' FirstName ');
$yourName->setlabel (' name ')->setrequired (True)->addvalidator (' Notempty ', true);
$email = new Zend_form_element_text (' email '); $email->setlabel (' E-mail 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 as a special class ... stored in an forms common directory. This makes it easier for us to manage all of our form forms. My implementation is to put it in the same level of directory as the controller (Controller) ... This is also convenient to manage. Of course, different friends. Have a different idea ... Another way. is to write it as a view Helper ... This way to achieve, but also very convenient. Here I don't want to write more ... Zend Framework is easy to implement ... As long as you want to ... No matter how you finish your task, it is OK. I'm not going to say anything more about it here. I just want to talk about how to let Zend_form realize the Chinese message function ... I have two ways to do this.

First: The more stupid way is: If your site does not make a multinational language website. At the same time your Zend Framework version is not often replaced ... You can find the source of the relevant information ... Changes to Chinese prompts.

This stupid method. There is no way to ... Oh...

Second: I am also in the English station ... A better way to see that is by rewriting this hint. Change it to the language we want ... So ... Even if we were going to change the language. or change the version of the Zend Framework.

The impact on us is not very big ... We'll just have to change our form to get it done. Now the code for this way is as follows (I only write an email message here.) Others do not write more):

<?php
$email = new Zend_form_element_text (' email ');
$email->setlabel (' E-mail 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 ' => ' This is not a valid email address! ', 
' emailaddressdotatom ' => ' This is not a valid email address! ', 
' emailaddressquotedstring ' => ' This is not a valid email address! ', 
' emailaddressinvalidlocalpart ' => ' This is not a valid email address! '. 
>

Come here. Zend_form This component also has a more important function. Is Zend_form_decorator. The manual is called an adorner, which means you can write the adorner you want yourself. For example.. You have to include your form in table. How do we achieve this? We need to use, say, Htmltag,label these adorners to achieve the function we want ... Here is a more important concept. Interested friends can go and have a look ... Because if you want to use zend_form this component. It will be difficult to use the adorner because it should be used. So it has to be this thing. To create the form functionality you want. At last.. It's just a little bit of a decorator.

I just implement a small function ... The following code:

<?php
$email = new Zend_form_element_text (' email ');
$email->setlabel (' E-mail address ')->addfilter (' Stringtolower ')->setrequired (FALSE)//use adorners to add TD tags
-> Adddecorator (' Htmltag ', array (' tag ' => ' TD '))->adddecorator (' Label ', array (' tag ' => ' TD ')) Reuse the Htmltag adorner to increase the TR tag
->adddecorator (Array (' FOOTR ' => ' Htmltag '), array (' tag ' => ' tr '))-> Addvalidator (' Notempty ');
? >

Ha ha.... The general application is this ... At last.. is to validate the submitted data ... See how to verify the data submitted by the user .... There's not much to say here ... Ok...

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

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.