Haohappy said in the article PEAR: HTML_QuickForm getting started in the third phase of PHPMore that he had never written a combination of QuickForm and Smarty. today, I saw a friend on the PHPE forum asking, I am ashamed of my laziness. I hope it will be helpful to you. In my opinion, PEAR: HTML_QuickForm is a very good form class library, SMARTY
Haohappy mentioned in the article PEAR: HTML_QuickForm getting started in the third phase of PHP & More that he has never written a combination of QuickForm and Smarty, today, I saw a friend on the PHPE forum asking, so I am ashamed of my laziness. I hope it will be helpful to you. In my opinion, PEAR: HTML_QuickForm is a very good form class library, which greatly accelerates development and is used by most projects. If you are not familiar with PEAR: HTML_QuickForm, we recommend that you read this article first.
This article is intended for PHP programmers with rich development experience and requires readers
1. Familiar with PEAR and its installation and use;
2. Familiar with HTML_QuickForm;
3. Understand the concept of a template and be familiar with the use of the Smarty template engine.
In the beautification output section of the Form in "PEAR: HTML_QuickForm entry", the built-in Form modification method of QuickForm is used to beautify the output. Obviously, this method seems a little troublesome, and it is a little difficult for us to make programmers beautify the web page. Currently, the most common collaboration between programmers and designers is through templates. therefore, how to combine QuickForm with the template engine is the problem we need to solve. In fact, QuickForm can be combined with multiple template engines, such as ITX, Sigma, Flexy, and Smarty. each template has its advantages and disadvantages. Currently, Smarty is the most common template engine, therefore, we will focus on the combination of QuickForm and Smarty.
First, let's take a look at our final results:
This example is very simple. there is only one Form and four inputs. it is only used to explain the usage of QuickForm. In actual development, we often encounter dozens of inputs. In fact, the more complex the form is, the more inefficient our traditional processing methods are, the more powerful QuickForm is. This may be realized in the future.
Well, start our QuickForm Smarty journey.
ChangPwd. php
Require_once ("des/config. inc. php ");
// Construct a Smarty object
$ Smarty = new Smarty_App;
$ Smarty-> assign ('cssdir', './templates/admin ');
$ Smarty-> assign ('title', ': Haohappy Test website management system ::');
// Construct the logon form
$ Form = new HTML_QuickForm ('frmchgpwd ', 'post ');
// Add a form element
$ Form-> addElement ('password', 'adminpwd', '', 'class = nameandpwd ');
$ Form-> addElement ('password', 'newpwd', '', 'class = nameandpwd ');
$ Form-> addElement ('password', 'newpwd2 ', '', 'class = nameandpwd ');
$ Form-> addElement ('submit ', 'btnsubmit', 'change password', 'class = btnSubmit ');
// Adding verification rules will automatically generate javascript variables and store them into javascript verification functions.
$ Form-> addRule ('adminpwd', 'the password cannot be blank! ', 'Requestred', '', 'client ');
$ Form-> addRule ('newpwd', 'the new password cannot be blank! ', 'Requestred', '', 'client ');
$ Form-> addRule ('newpwd2 ',' the new password cannot be blank! ', 'Requestred', '', 'client ');
$ Form-> addRule (array ('newpwd', 'newpwd2 '), "the two passwords are different !! ", 'Company','', 'client ');
If ($ form-> validate ()){
// Change the password if the form data is correct
$ Form-> process ('changepwd ');
}
Else {
// Otherwise, the form is displayed.
// Create a renderer object
$ Renderer = & new HTML_QuickForm_Renderer_ArraySmarty ($ smarty );
// Build the HTML for the form generated HTML code
$ Form-> accept ($ renderer );
// Assign array with form data allocates form data to the array
$ Smarty-> assign ('form _ data', $ renderer-> toArray ());
$ Smarty-> catching = false;
// Debug
// Echo"
";var_dump($renderer->toArray());echo "
";
$ Smarty-> display ("changePwd. tpl ");
}
// Change the password
Function changePwd (){}
?>
In the code, we added four form elements with $ form-> addElement () and added four verification rules with $ form-> addRule. How about it? is it fast and convenient? Taking verification rules that verify the two passwords are the same as an example, if we write verification rules by ourselves, although fast, the code will look bloated and messy, and QuickForm will be responsible for data verification, the development speed is greatly improved, and the code looks concise and beautiful. Only one line of code is used:
$ Form-> addRule (array ('newpwd', 'newpwd2 '), "the two passwords are different !! ", 'Company','', 'client ');
For the benefits of QuickForm, see PEAR: HTML_QuickForm getting started.
The following code links HTML_QuickForm to Smarty:
$ Renderer = & new HTML_QuickForm_Renderer_ArraySmarty ($ smarty );
The so-called renderer is used for display. here we specify the renderer of QuickForm as Smarty, and we can use the powerful Smarty template engine to format the output of QuickForm.
Others:
ChangePwd () is the core operation function of this file, used to change the password.
$ Form-> process ('changepwd '); // This line of code is used to call changePwd ()
// Echo"
";var_dump($renderer->toArray());echo "
";
This line is used for debugging. we can print all the variables in $ renderer at any time to see if the program is correctly executed.
Let's look at our template, which is also very simple:
ChangePwd. tpl
{If $ form_data.javascript}
{$ Form_data.javascript}
{/If}
These two simple files, with less than 100 lines of code in total, have completed our results at the beginning of the article. Contains the complete form data verification and processing process.
In addition, quick form can be used to easily separate the display layer from the logic layer, because the processing functions are completely independent.
For example
If ($ form-> validate ()){
// Change the password if the form data is correct
$ Form-> process ('changepwd ');
}
Converted
If ($ form-> validate ()){
Switch ($ post_vars ['action']) {
Default:
Case "changPwd ":
$ Form-> process ('changepwd ');
Break;
Case "Add ":
$ Form-> process ('ADD ');
Break;
Case "Update ":
$ Form-> process ('update ');
Break;
Case "Delete ":
$ Form-> process ('delete ');
Break;
}
Then, the changePwd, add, update, and delete functions are independent to a file. In this way, you can call different operations based on the actions submitted on the page.
This idea is relatively simple. if you want to use more powerful functions, you can try again.
PEAR: HTML_QuickForm_Controller. HTML_QuickForm_Controller is based on the PageController design mode, that is, a single page is used to process requests and actions passed through GET and POST. This is a very interesting idea, but this development mode is not suitable for beginners, because it is relatively complicated. The author also said:
HTML_QuickForm_Controller is not intended for PHP newbies. if you don't understand what classes are, if you have no prior experience with QuickForm, if you are a fan of copy-paste programming then this package is not for you.
The package is indeed complex, but so are the problems it is trying to solve. try to rewrite any of the enclosed multipage form examples without using such a package and you'll see what we mean.
This part is not discussed for the time being. we need to have some knowledge about MVC and try to write another article next time. My current development framework is an MVC framework. I think it is quite easy. The Display part is handed over to Smarty QuickForm. I hope more people will pay attention to and use QuickForm. I believe that we will not be disappointed after using QuickForm.