Home Backend Development PHP Tutorial PEAR::HTML_QuickForm与Smarty 的结合应用_PHP

PEAR::HTML_QuickForm与Smarty 的结合应用_PHP

Jun 01, 2016 pm 12:31 PM
i password application us combine form

SMARTY

  Haohappy在《PHP & More》第三期的《PEAR::HTML_QuickForm入门》一文中说过要写QuickForm和Smarty的结合应用,一直没写,今天看到PHPE论坛上有朋友在询问,为自己的懒惰而惭愧。现在补上,希望对大家有点帮助。 在我看来,PEAR::HTML_QuickForm是个非常优秀的表单类库,大大加快了开发速度,我现在的大多数项目都会用到。如果对PEAR::HTML_QuickForm不了解的朋友,建议先看这篇文章。

  本文针对的读者为有较丰富开发经验的PHP程序员,要求读者

1. 熟悉PEAR及其安装和使用;

2. 熟悉HTML_QuickForm;

3. 理解模板的概念,熟悉Smarty模板引擎的使用。

  在《PEAR::HTML_QuickForm入门》的表单的美化输出一节中,提到了用QuickForm自带的Form修饰方法来美化输出。很明显,这种方法显得有点麻烦,而且让程序员来美化网页,有点难为我们了。 现在程序员和设计师的合作最常见的就是通过模板,所以如何把QuickForm和模板引擎相结合,这就是我们需要解决的问题。其实QuickForm可以和多种模板引擎相结合,如ITX, Sigma, Flexy, Smarty等,每种模板都有其优点和缺点,目前Smarty是最通用的模板引擎,所以我们把QuickForm和Smarty的结合作为重点来研究。

  首先,给大家看看我们的最后效果:

PEAR::HTML_QuickForm与Smarty 的结合应用_PHP

  这个例子非常简单,只有一个Form,4个Input,只是用来讲解QuickForm的使用。在实际开发中,我们经常遇到几十个Input的情况。实际上,表单越复杂,就越显出我们传统的处理方式的低效,就越显出QuickForm的强大。这一点,也许大家以后会体会到。

好,开始我们的QuickForm Smarty之旅。

changPwd.php


require_once("includes/config.inc.php");
//构建Smarty对象
$smarty = new Smarty_App;
$smarty->assign('CSSDIR','./templates/admin');
$smarty->assign('title',':: Haohappy Test网站管理系统 ::');

//构建登录表单
$form = new HTML_QuickForm('frmChgPwd', 'post');

//增加表单元素
$form->addElement('password', 'adminPwd', '','class = NameAndPwd');
$form->addElement('password', 'newPwd', '','class = NameAndPwd');
$form->addElement('password', 'newPwd2', '','class = NameAndPwd');
$form->addElement('submit', 'btnSubmit', '修改密码','class = btnSubmit');

//增加验证规则 会自动生成javascript变量,存入javascript验证函数
$form->addRule('adminPwd','密码不能为空!', 'required','','client');
$form->addRule('newPwd','新密码不能为空!', 'required','','client');
$form->addRule('newPwd2','新密码不能为空!', 'required','','client');
$form->addRule(array('newPwd','newPwd2'),"两次输入的密码不同!!",'compare','','client');

if ($form->validate()) {
//如果表单数据正确,修改密码
$form->process('changePwd');
}
else{

//否则显示表单

// 建立renderer对象
$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($smarty );

// build the HTML for the form 生成表单的HTML代码
$form->accept($renderer);

//assign array with form data 分配表单数据到数组中
$smarty->assign('form_data', $renderer->toArray());
$smarty->catching = false;

// 调试
//echo "

";var_dump($renderer->toArray());echo "
Copy after login
Copy after login
";
$smarty->display("changePwd.tpl");

}

//修改密码
function changePwd(){}
?>

  在代码中,我们用$form->addElement()增添了4个表单元素,用$form->addRule()增加了4条验证规则。怎么样,是不是很快捷方便? 以验证两个密码是否相同的验证规则为例,如果我们自己写验证规则,虽然快,但是代码就会显得臃肿和凌乱,由QuickForm来负责数据验证,开发速度大大提高,而且代码显得很简洁漂亮。仅用了一行代码:

$form->addRule(array('newPwd','newPwd2'),"两次输入的密码不同!!",'compare','','client');

  关于QuickForm的好处,请参看《PEAR::HTML_QuickForm入门》,在此不再重复。

  下面这行代码就是我们将HTML_QuickForm与Smarty连接的桥梁:

$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($smarty );

  所谓renderer,就是用来负责显示的,这里我们把QuickForm的renderer指定为Smarty,我们就可以使用强大的Smarty模板引擎来格式化QuickForm的输出了。

  其它:

changePwd()是这个文件的核心操作函数,用来修改密码。

$form->process('changePwd'); //这行代码用来调用changePwd()


//echo "

";var_dump($renderer->toArray());echo "
Copy after login
Copy after login
";

  这一行用来调试,我们随时可以把$renderer中的所有变量打印出来,看看程序是否执行正确。

  再看我们的模板,也很简单:

changePwd.tpl

{if $form_data.javascript}

{$form_data.javascript}

{/if}

 

 

 

修改管理员密码
现有管理员密码

{$form_data.adminPwd.html}
新密码

{$form_data.newPwd.html}
再次输入新密码

{$form_data.newPwd2.html}

{$form_data.btnSubmit.html}

  这两个简单的文件,总共不到100行代码,就完成了我们在文章开头的效果。包含完整的表单数据验证,处理过程。

  另:使用QuickForm,可以很方便地实现显示层和逻辑层的分离,因为处理的函数是完全独立出来的。

  例如说可以把

if ($form->validate()) {
//如果表单数据正确,修改密码
$form->process('changePwd');
}

改装成

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;

}

  然后把changePwd,add,update,delete四个函数独立到某个文件当中。这样就可以根据页面提交的action来调用不同的操作。

  这个想法相对比较简陋一些,如果你要用更强大的功能,还可以试试

  PEAR::HTML_QuickForm_Controller。HTML_QuickForm_Controller基于PageController设计模式,也就是用单个页面来处理通过GET和POST传递而来的request和 action。这是个非常有意思的想法,但是这种开发模式不适合于新手,因为相对比较复杂。其作者也说:

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.

  这部份暂不讨论,这需要大家对MVC有一定了解,下次有机会再另写文章。我自己目前的开发框架就是个MVC框架,觉得很顺手,显示部份就是交给Smarty QuickForm来完成。希望有更多人关注和使用QuickForm,相信使用之后大家一定不会失望的

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem that Windows 11 prompts you to enter the administrator username and password to continue? How to solve the problem that Windows 11 prompts you to enter the administrator username and password to continue? Apr 11, 2024 am 09:10 AM

When using Win11 system, sometimes you will encounter a prompt that requires you to enter the administrator username and password. This article will discuss how to deal with this situation. Method 1: 1. Click [Windows Logo], then press [Shift+Restart] to enter safe mode; or enter safe mode this way: click the Start menu and select Settings. Select "Update and Security"; select "Restart Now" in "Recovery"; after restarting and entering the options, select - Troubleshoot - Advanced Options - Startup Settings -&mdash

How to set router WiFi password using mobile phone (using mobile phone as tool) How to set router WiFi password using mobile phone (using mobile phone as tool) Apr 24, 2024 pm 06:04 PM

Wireless networks have become an indispensable part of people's lives in today's digital world. Protecting the security of personal wireless networks is particularly important, however. Setting a strong password is key to ensuring that your WiFi network cannot be hacked by others. To ensure your network security, this article will introduce in detail how to use your mobile phone to change the router WiFi password. 1. Open the router management page - Open the router management page in the mobile browser and enter the router's default IP address. 2. Enter the administrator username and password - To gain access, enter the correct administrator username and password in the login page. 3. Navigate to the wireless settings page - find and click to enter the wireless settings page, in the router management page. 4. Find the current Wi

The role and practical application of arrow symbols in PHP The role and practical application of arrow symbols in PHP Mar 22, 2024 am 11:30 AM

The role and practical application of arrow symbols in PHP In PHP, the arrow symbol (->) is usually used to access the properties and methods of objects. Objects are one of the basic concepts of object-oriented programming (OOP) in PHP. In actual development, arrow symbols play an important role in operating objects. This article will introduce the role and practical application of arrow symbols, and provide specific code examples to help readers better understand. 1. The role of the arrow symbol to access the properties of an object. The arrow symbol can be used to access the properties of an object. When we instantiate a pair

Tutorial on changing wifi password on mobile phone (simple operation) Tutorial on changing wifi password on mobile phone (simple operation) Apr 26, 2024 pm 06:25 PM

Wireless networks have become an indispensable part of our lives with the rapid development of the Internet. In order to protect personal information and network security, it is very important to change your wifi password regularly, however. To help you better protect your home network security, this article will introduce you to a detailed tutorial on how to use your mobile phone to change your WiFi password. 1. Understand the importance of WiFi passwords. WiFi passwords are the first line of defense to protect personal information and network security. In the Internet age, understanding its importance can better understand why passwords need to be changed regularly. 2. Confirm that the phone is connected to wifi. First, make sure that the phone is connected to the wifi network whose password you want to change before changing the wifi password. 3. Open the phone’s settings menu and enter the phone’s settings menu.

Incorrect password, beware BitLocker warning Incorrect password, beware BitLocker warning Mar 26, 2024 am 09:41 AM

This article will explore how to solve the problem of wrong password, especially the need to be careful when dealing with BitLocker warnings. This warning is triggered when an incorrect password is entered multiple times in BitLocker to unlock the drive. Usually, this warning occurs because the system has a policy that limits incorrect login attempts (usually three login attempts are allowed). In this case, the user will receive an appropriate warning message. The complete warning message is as follows: The password entered is incorrect. Please note that continuously entering incorrect passwords will cause the account to be locked. This is to protect the security of your data. If you need to unlock your account, you will need to use a BitLocker recovery key. The password is incorrect, beware the BitLocker warning you receive when you log in to your computer

How to Undo Delete from Home Screen in iPhone How to Undo Delete from Home Screen in iPhone Apr 17, 2024 pm 07:37 PM

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

What is the name of the software that can unlock all wifi passwords (recommended mobile software to get connected wifi passwords with one click) What is the name of the software that can unlock all wifi passwords (recommended mobile software to get connected wifi passwords with one click) Apr 02, 2024 pm 06:46 PM

Wireless networks have become an integral part of people's lives in modern society. Or need to view saved passwords in WiFi list, sometimes we forget WiFi password, however. Mobile software came into being to solve this problem. To make it easy for you to master this magical tool, this article will introduce a software that can help you quickly unlock all WiFi passwords. 1. Powerful WiFi password unlocking tool - explore the wonderful world of WiFi password unlocking 2. Simple and easy-to-use interface design - convenient and fast operation methods 3. Supports multiple devices - meets the needs of different devices 4. Automatically Update - always keep the latest WiFi password 5. High-speed WiFi connection - easily cope with the simultaneous connection needs of multiple devices

How to enter the system if you forget your win10 computer power-on password_What to do if you forget your win10 computer power-on password How to enter the system if you forget your win10 computer power-on password_What to do if you forget your win10 computer power-on password Mar 28, 2024 pm 02:35 PM

1. Download and install Xiaobai’s one-click system reinstallation tool on another computer, insert an empty USB disk to create a USB boot disk. For specific tutorials, please refer to: 2. Insert the USB boot disk into the computer that needs to change the password to restart, and press Start hotkey. Generally, the startup hotkey is one of F12, F8, F9, F10, and ESC. Then the startup interface appears, select the option of the USB disk and press Enter to enter. 3. Select [1] to start win10x64PE and press Enter to confirm. 4. Select the password modification tool on the desktop and double-click to open it. 5. Then a list of account names appears, select the account that needs to change the password and open it. 6. Click the Change Password command below, enter the new password twice, and then click OK to save the changes. 7. Finally, unplug the USB flash drive and restart the computer. Then it will be normal.

See all articles