Smarty+QUICKFORM小小演示
更新時間:2007年02月25日 00:00:00 作者:
由于公司需要quickform結(jié)合SMARTY的開發(fā)模式,最近幾天惡補(bǔ)了下,跟大家分享下心得吧,quickform是一個PEAR類庫,可以快速生成表單控件及驗(yàn)證表單的JS代碼,大家可能覺得這個用手寫JS和HTML生成不是很快嗎,用那個不是更麻煩,的確,少量的表單控件是顯示不出quickform的優(yōu)勢的,但是如果有大量的表單控件,例如OA的后臺,quickform的優(yōu)勢就顯示出來了,利用quickform有代碼清晰,易于維護(hù)等特點(diǎn),非常適合大中型項(xiàng)目的開發(fā),更方便的是可以在smarty中輕松使用它,^_^廢話少說,來看看代碼,不過大家之前最好了解下PEAR的安裝,參照:http://hi.baidu.com/wanghaozi/blog/item/81cfb7003f973687e850cd3e.html。
由于公司用的quickform是自己改進(jìn)過的,因此代碼和大家網(wǎng)上看到的會有些差別,涉及版權(quán)在這里就不便說明,簡要展示下核心代碼,大家就當(dāng)了解下吧,有興趣的朋友可以看看這篇HAOHAPPY的文章:http://www.phpe.net/articles/418.shtml
[php]
<?php
/*
*作者:輝老大
*頁面:path.cfg.php
*功能:系統(tǒng)路徑設(shè)置
*版權(quán)所有:隨便copy^_^
*/
$global['path']['conf'] = $global['path']['root'] . 'conf/';//定義系統(tǒng)配置文件路徑
$global['path']['lib'] = $global['path']['root'] . 'lib/';//定義系統(tǒng)庫文件路徑
?>
[/php]
[php]
<?php
/*
*作者:輝老大
*頁面:smarty.cfg.php
*功能:smarty基本配置
*版權(quán)所有:隨便copy^_^
*/
//定義模板路徑
$global['smarty']['template_dir'] = $global['path']['root'] . 'lib/smarty/templates';
//定義模板編譯目錄
$global['smarty']['compile_dir'] = $global['path']['root'] . 'lib/smarty/templates_c';
//定義smarty配置文件夾路徑
$global['smarty']['config_dir'] = $global['path']['conf'] . 'lib/smarty/configs';
$global['smarty']['cache_dir'] = $global['path']['root'] . 'lib/smarty/cache';
//$global['smarty']['compile_check'] = true;
//設(shè)置smarty報(bào)錯禁用
$global['smarty']['debugging'] = false;
//關(guān)閉緩存
$global['smarty']['caching'] = false;
//$global['smarty']['cache_lifetime'] = 6000;
//定義左右邊界符
$global['smarty']['left_delimiter'] = '<{';
$global['smarty']['right_delimiter'] = '}>';
?>
[/php]
[php]
<?php
/*
*作者:輝老大
*頁面:common.cfg.php
*功能:全局配置
*版權(quán)所有:隨便copy^_^
*/
$global['path']['root'] = dirname(__FILE__) . '/';//設(shè)置根目錄
require($global['path']['conf'] . 'conf/path.cfg.php');
require($global['path']['conf'] . 'smarty.cfg.php');
//包含smarty類庫
require($global['path']['lib'] . 'smarty/libs/Smarty.class.php');
//smarty配置
$tpl = new Smarty();
$tpl->template_dir = $global['smarty']['template_dir'];
$tpl->compile_dir = $global['smarty']['compile_dir'];
$tpl->config_dir = $global['smarty']['config_dir'];
$tpl->debugging = $global['smarty']['debugging'];
$tpl->caching = $global['smarty']['caching'];
$tpl->cache_lifetime = $global['smarty']['cache_lifetime'];
$tpl->left_delimiter = $global['smarty']['left_delimiter'];
$tpl->right_delimiter = $global['smarty']['right_delimiter'];
unset($global['smarty']);
ini_set('include_path', ini_get('include_path') .
PATH_SEPARATOR . $global['path']['lib'] . 'pear/');//載入pear庫文件
?>
[/php]
[php]
<?php
/*
*作者:輝老大
*頁面:index.php
*功能:UI
*版權(quán)所有:隨便copy^_^
*/
require_once('common.inc.php');//載入全局配置
//包含quickform類庫
require($global['path']['lib'] . 'pear/HTML/QuickForm.php');
$form = new HTML_QuickForm('changepwdform');//生成quickform實(shí)例,參數(shù)為表單名
/*
*開始添加表單元素
*參數(shù)依次為:表單元素類型,名稱,(按鈕標(biāo)簽文字),樣式
*/
$form->addElement('password','adminPwd','','style="width:120px"');
$form->addElement('password','newPwd','','style="width:120px"');
$form->addElement('password','newPwd2','','style="width:120px"');
$form->addElement('submit','btnSubmit','修改密碼','style="width:100px"');
//增加驗(yàn)證規(guī)則,自動生成JS
$form->addRule('adminPwd','密碼不能為空!','required','','client');
$form->addRule('newPwd','新密碼不能為空!','required','','client');
$form->addRule('newPwd2','請?jiān)俅屋斎胄旅艽a!','required','client');
$form->addRule(array('newPwd','newPwd2'),"兩次輸入的密碼不一致!",'compare','','client');
$form->;//禁止提交表單
//分配表單數(shù)據(jù)到數(shù)組中
$tpl->assign('form_data',$form->toArray());
//顯示模板
$tpl->display('index.tpl');
?>
[/php]
模板代碼:
<HTML>
<HEAD>
<TITLE>quickform+smarty</TITLE>
<{if $form_data.javascrīpt}>
<{$form_data.javascrīpt}>
<{/if}>
</HEAD>
<BODY>
<p> </p>
<p> </p>
<p> </p>
<form <{$form_data.attributes}> >
<table width="300" border="0" align="center" cellpadding="3" cellspacing="3"
bgcolor="#F6F6F6" style="font-size:9pt" class="AddTable">
<tr bgcolor="#FFFFFF">
<td width="47%" colspan="2"><div align="center">修改管理員密碼</div></tr>
<tr>
<tr>
<td width="47%"><div align="center">現(xiàn)有管理員密碼
</div></td>
<td width="53%"><{$form_data.adminPwd.html}></td>
</tr>
<tr>
<td><div align="center">新密碼
</div></td>
<td><{$form_data.newPwd.html}></td>
</tr>
<tr>
<td><div align="center">再次輸入新密碼
</div></td>
<td><{$form_data.newPwd2.html}></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<{$form_data.btnSubmit.html}>
</div></td>
</tr>
</table>
</form>
<scrīpt type="text/javascrīpt" src="response.js"></scrīpt>
</BODY>
</HTML>
這里大家也許覺得奇怪,為什么路徑要定義這么復(fù)雜,而且使用絕對路徑呢?這個是最近適應(yīng)公司項(xiàng)目的需要,呵呵!其實(shí)這樣有利于部署大的項(xiàng)目。這個帖子相信沒接觸過quickform或smarty的新手一定看的一頭霧水,當(dāng)然,我在這也只是簡單介紹下,希望大家有興趣的可以繼續(xù)深入研究,最后看看效果:
看判斷兩次輸入密碼是否一樣就這一句:
[php]
$form->addRule(array('newPwd','newPwd2'),"兩次輸入的密碼不一致!",'compare','','client');
[/php]
代碼看起來是不是簡潔清楚啊,呵呵,接下來還會應(yīng)用到再結(jié)合XAJAX的應(yīng)用,我會繼續(xù)和大家分享學(xué)習(xí)心得,嘿嘿!
由于公司用的quickform是自己改進(jìn)過的,因此代碼和大家網(wǎng)上看到的會有些差別,涉及版權(quán)在這里就不便說明,簡要展示下核心代碼,大家就當(dāng)了解下吧,有興趣的朋友可以看看這篇HAOHAPPY的文章:http://www.phpe.net/articles/418.shtml
[php]
<?php
/*
*作者:輝老大
*頁面:path.cfg.php
*功能:系統(tǒng)路徑設(shè)置
*版權(quán)所有:隨便copy^_^
*/
$global['path']['conf'] = $global['path']['root'] . 'conf/';//定義系統(tǒng)配置文件路徑
$global['path']['lib'] = $global['path']['root'] . 'lib/';//定義系統(tǒng)庫文件路徑
?>
[/php]
[php]
<?php
/*
*作者:輝老大
*頁面:smarty.cfg.php
*功能:smarty基本配置
*版權(quán)所有:隨便copy^_^
*/
//定義模板路徑
$global['smarty']['template_dir'] = $global['path']['root'] . 'lib/smarty/templates';
//定義模板編譯目錄
$global['smarty']['compile_dir'] = $global['path']['root'] . 'lib/smarty/templates_c';
//定義smarty配置文件夾路徑
$global['smarty']['config_dir'] = $global['path']['conf'] . 'lib/smarty/configs';
$global['smarty']['cache_dir'] = $global['path']['root'] . 'lib/smarty/cache';
//$global['smarty']['compile_check'] = true;
//設(shè)置smarty報(bào)錯禁用
$global['smarty']['debugging'] = false;
//關(guān)閉緩存
$global['smarty']['caching'] = false;
//$global['smarty']['cache_lifetime'] = 6000;
//定義左右邊界符
$global['smarty']['left_delimiter'] = '<{';
$global['smarty']['right_delimiter'] = '}>';
?>
[/php]
[php]
<?php
/*
*作者:輝老大
*頁面:common.cfg.php
*功能:全局配置
*版權(quán)所有:隨便copy^_^
*/
$global['path']['root'] = dirname(__FILE__) . '/';//設(shè)置根目錄
require($global['path']['conf'] . 'conf/path.cfg.php');
require($global['path']['conf'] . 'smarty.cfg.php');
//包含smarty類庫
require($global['path']['lib'] . 'smarty/libs/Smarty.class.php');
//smarty配置
$tpl = new Smarty();
$tpl->template_dir = $global['smarty']['template_dir'];
$tpl->compile_dir = $global['smarty']['compile_dir'];
$tpl->config_dir = $global['smarty']['config_dir'];
$tpl->debugging = $global['smarty']['debugging'];
$tpl->caching = $global['smarty']['caching'];
$tpl->cache_lifetime = $global['smarty']['cache_lifetime'];
$tpl->left_delimiter = $global['smarty']['left_delimiter'];
$tpl->right_delimiter = $global['smarty']['right_delimiter'];
unset($global['smarty']);
ini_set('include_path', ini_get('include_path') .
PATH_SEPARATOR . $global['path']['lib'] . 'pear/');//載入pear庫文件
?>
[/php]
[php]
<?php
/*
*作者:輝老大
*頁面:index.php
*功能:UI
*版權(quán)所有:隨便copy^_^
*/
require_once('common.inc.php');//載入全局配置
//包含quickform類庫
require($global['path']['lib'] . 'pear/HTML/QuickForm.php');
$form = new HTML_QuickForm('changepwdform');//生成quickform實(shí)例,參數(shù)為表單名
/*
*開始添加表單元素
*參數(shù)依次為:表單元素類型,名稱,(按鈕標(biāo)簽文字),樣式
*/
$form->addElement('password','adminPwd','','style="width:120px"');
$form->addElement('password','newPwd','','style="width:120px"');
$form->addElement('password','newPwd2','','style="width:120px"');
$form->addElement('submit','btnSubmit','修改密碼','style="width:100px"');
//增加驗(yàn)證規(guī)則,自動生成JS
$form->addRule('adminPwd','密碼不能為空!','required','','client');
$form->addRule('newPwd','新密碼不能為空!','required','','client');
$form->addRule('newPwd2','請?jiān)俅屋斎胄旅艽a!','required','client');
$form->addRule(array('newPwd','newPwd2'),"兩次輸入的密碼不一致!",'compare','','client');
$form->;//禁止提交表單
//分配表單數(shù)據(jù)到數(shù)組中
$tpl->assign('form_data',$form->toArray());
//顯示模板
$tpl->display('index.tpl');
?>
[/php]
模板代碼:
復(fù)制代碼 代碼如下:
<HTML>
<HEAD>
<TITLE>quickform+smarty</TITLE>
<{if $form_data.javascrīpt}>
<{$form_data.javascrīpt}>
<{/if}>
</HEAD>
<BODY>
<p> </p>
<p> </p>
<p> </p>
<form <{$form_data.attributes}> >
<table width="300" border="0" align="center" cellpadding="3" cellspacing="3"
bgcolor="#F6F6F6" style="font-size:9pt" class="AddTable">
<tr bgcolor="#FFFFFF">
<td width="47%" colspan="2"><div align="center">修改管理員密碼</div></tr>
<tr>
<tr>
<td width="47%"><div align="center">現(xiàn)有管理員密碼
</div></td>
<td width="53%"><{$form_data.adminPwd.html}></td>
</tr>
<tr>
<td><div align="center">新密碼
</div></td>
<td><{$form_data.newPwd.html}></td>
</tr>
<tr>
<td><div align="center">再次輸入新密碼
</div></td>
<td><{$form_data.newPwd2.html}></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<{$form_data.btnSubmit.html}>
</div></td>
</tr>
</table>
</form>
<scrīpt type="text/javascrīpt" src="response.js"></scrīpt>
</BODY>
</HTML>
這里大家也許覺得奇怪,為什么路徑要定義這么復(fù)雜,而且使用絕對路徑呢?這個是最近適應(yīng)公司項(xiàng)目的需要,呵呵!其實(shí)這樣有利于部署大的項(xiàng)目。這個帖子相信沒接觸過quickform或smarty的新手一定看的一頭霧水,當(dāng)然,我在這也只是簡單介紹下,希望大家有興趣的可以繼續(xù)深入研究,最后看看效果:
看判斷兩次輸入密碼是否一樣就這一句:
[php]
$form->addRule(array('newPwd','newPwd2'),"兩次輸入的密碼不一致!",'compare','','client');
[/php]
代碼看起來是不是簡潔清楚啊,呵呵,接下來還會應(yīng)用到再結(jié)合XAJAX的應(yīng)用,我會繼續(xù)和大家分享學(xué)習(xí)心得,嘿嘿!
相關(guān)文章
使用php轉(zhuǎn)義輸出HTML到JavaScript
本文給大家分享的是個人項(xiàng)目中的一個小需求,需要使用php轉(zhuǎn)義輸出HTML到JavaScript,就寫了個function,推薦給大家,希望大家能夠喜歡。2015-03-03php中$_SERVER[PHP_SELF] 和 $_SERVER[SCRIPT_NAME]之間的區(qū)別
php中$_SERVER[PHP_SELF] 和 $_SERVER[SCRIPT_NAME]之間的區(qū)別2009-09-09PHP實(shí)現(xiàn)基于棧的后綴表達(dá)式求值功能
這篇文章主要介紹了PHP實(shí)現(xiàn)基于棧的后綴表達(dá)式求值功能,簡單描述了后綴表達(dá)式的概念并結(jié)合實(shí)例形式分析了php使用棧實(shí)現(xiàn)后綴表達(dá)式求值的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11php array_walk() 數(shù)組函數(shù)
函數(shù)array_walk():單一數(shù)組回調(diào)函數(shù)---對數(shù)組中的每個成員應(yīng)用用戶函數(shù)2011-07-07php實(shí)現(xiàn)獲取本年,本月,本周時間戳和日期格式
這篇文章主要為大家詳細(xì)介紹了php實(shí)現(xiàn)獲取本年、本月、本周時間戳和日期格式的相關(guān)方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2023-12-12