使用cakephp进行Forms确认
时间:2006-11-08
来源:互联网
确认data 你已经发现了,我们在以前并没有太多的举例submit按钮事件。如果你查看form作用属性,你会发现相同的脚本命名。为了让使用更简单,HTML_QuickForm 自带参考,所以,同样的脚本是为了生成form,验证它,并且成功之后处理它。为了促使事件发生,我们需要添加logic去处理提交表单,并且确认数据。
验证是许多开发者的弱项,但是HTML_QuickForm使无论在客户端还是在服务器端的验证都非常的简单。它能做到这些是通过addRule()方式,之后,执行验证,用validate()方式。我们现在只能显示没有被验证的表单(因为在这个时候处理表单的代码能够被访问)。一切都很简单!这个例子有两个规则,一个是first name是必须填写的内容,第二个是first name必须只包含字母(并且不能是符号,所以就使得提交申请的人认真对待!):
<?
require_once "HTML/QuickForm.php";
$form = new HTML_QuickForm('register', 'post');
$form->addElement('text', 'firstName', 'Enter first name');
$form->addElement('password','password', 'Enter your password');
$form->addElement('textarea','ta','Description');
$form->addElement('submit','sb','Submit form');
$form->addRule('firstName', 'Your name is required', 'required');
$form->addRule('firstName', 'Your name can only contain letters','lettersonly');
if ($form->validate()) {
// processing code goes here.
echo 'Success!';
}
else {
$form->display();
}
?>
试着去看看当你用无效数据提交这个表单时会发生什么,然后用有效数据试一次,一个红色的*指出first name是需要填写的。如果数据被不正确地填写(左边有空格,或有除了字母以外的其他符号)一个适当的出错信息会用红色显示出来。
以上例子应用了required和lettersonly规则。以下列出了可被应用到的所有的规则:
rule argument description
alphanumeric Can only contain alphanumeric characters (letters and numbers).
compare Compares two values.
email true (which applies a DNS check) Must be a valid email (in syntax) (checks for a valid hostname if the argument is set to true).
filename $regex The filename of the uploaded file must match the regular expression in $regex.
lettersonly Can only contain letters.
maxfilesize $maxsize The filename of the uploaded file cannot be larger than $maxsize bytes.
maxlength $maxlength Can be at most $maxlength characters in length.
mimetype $mimetype MIME type of the uploaded file must either be of type $mimetype (if $mimetype is scalar), or match one of the elements in $mimetype (if it's an array).
minlength $minlength Must be at least $minlength in length.
numeric Must contain a valid integer or decimal number.
nonzero Cannot be zero.
nopunctuation Cannot contain any punctuation characters, which include: ( ) . / * ^ ? # ! @ $ % + = , " ' > < ~ [ ] { }.
rangelength $minlength,$maxlength Must be inclusively between $minlength and $maxlength characters in length.
regex $regex Must match the regular expression $regex.
required Cannot be blank
uploadedfile Must contain a successfully uploaded file.
让我们看看一些例子和特列。一个非常有用的特列是,你也可以在客户端验证,用"client"标记。在客户端进行验证是很好的做法,因为可使得用户的使用更加的简单(他们不需要等待服务器的应答),但是你通常还是需要在服务器端进行验证,因为并不是所有客户端都有JavaScript激活功能。我们同样着眼于comparison规则(在这个例子中两个空格不能一样),和一个custom规则,它允许你用你的想要的标准去写一个函数:
<?
require_once "HTML/QuickForm.php";
$form = new HTML_QuickForm('register', 'post');
$form->addElement('text', 'firstName', 'Enter first name');
$form->addElement('password','password', 'Enter your password');
$form->addElement('text','customer_email','Enter an email');
$form->addElement('textarea','ta','Description');
$form->addElement('submit','sb','Submit form');
$form->addRule('firstName', 'Your name is required', 'required');
$form->addRule('firstName', 'Your name can only contain letters','lettersonly');
$form->addRule('firstName', 'You must enter a first name', 'required', null, 'client');
$form->addRule('firstName', 'Your name cannot be less than two characters!','minlength',2,'client');
$form->addRule('customer_email', 'Please enter a valid email','email',true,'client');
$form->AddRule(array('password','firstName'),'Your password and first name cannot be the same!','compare','!=');
$form->registerRule('no_symbol','function','no_symbol_f');
$form->addRule('firstName','Sorry, your name cannot be Symbol','no_symbol');
if ($form->validate()) {
// processing code goes here.
echo 'Success!';
}
else {
$form->display();
}
function no_symbol_f($element_name,$element_value) {
if ($element_value == 'Symbol') {
return false;
}
else {
return true;
}
}
?>
以下是所要产生:
<script type="text/javascript">
//<![CDATA[
function validate_register(frm) {
var value = '';
var errFlag = new Array();
var _qfGroups = {};
_qfMsg = '';
value = frm.elements['firstName'].value;
if (value == '' && !errFlag['firstName']) {
errFlag['firstName'] = true;
_qfMsg = _qfMsg + '\n - You must enter a first name';
}
value = frm.elements['firstName'].value;
if (value != '' && value.length < 2 && !errFlag['firstName']) {
errFlag['firstName'] = true;
_qfMsg = _qfMsg + '\n - Your name cannot be less than two characters!';
}
value = frm.elements['customer_email'].value;
var regex = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
if (value != '' && !regex.test(value) && !errFlag['customer_email']) {
errFlag['customer_email'] = true;
_qfMsg = _qfMsg + '\n - Please enter a valid email';
}
if (_qfMsg != '') {
_qfMsg = 'Invalid information entered.' + _qfMsg;
_qfMsg = _qfMsg + '\nPlease correct these fields.';
alert(_qfMsg);
return false;
}
return true;
}
//]]>
</script>
<form action="/phpbuilder/html_quickform33.php" method="post" name="register" id="register" >
<div>
<table border="0">
<tr>
<td align="right" valign="top"><span style="color: #ff0000">*</span><b>Enter first name</b></td>
<td valign="top" align="left"> <input name="firstName" type="text" /></td>
</tr>
<tr>
<td align="right" valign="top"><b>Enter your password</b></td>
<td valign="top" align="left"> <input name="password" type="password" /></td>
</tr>
<tr>
<td align="right" valign="top"><b>Enter an email</b></td>
<td valign="top" align="left"> <input name="customer_email" type="text" /></td>
</tr>
<tr>
<td align="right" valign="top"><b>Description</b></td>
<td valign="top" align="left"> <textarea name="ta"></textarea></td>
</tr>
<tr>
<td align="right" valign="top"><b></b></td>
<td valign="top" align="left"> <input name="sb" value="Submit form" type="submit" /></td>
</tr>
<tr>
<td></td>
<td align="left" valign="top"><span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span></td>
</tr>
</table>
</div>
</form>
在这个例子中,compare规则允许其他操作者使用非等(!=),或者<or>。如果你把操作者一起省去,它被假定为=,意思是这两个元素都必须相同(特别地适合去检查用户输入的密码或邮件地址是否正确)
处理表单数据
迄今为止,我们已经简单地成功显示了!如果表单已经被验证。虽然它看起来并不是特别的优秀。HTML_QuickForm 提供一个process()方式,它调用一个函数并审核提交值。以下是操作方式:
<?
require_once "HTML/QuickForm.php";
$defaults_= array("firstName"=>"Ian",
"password"=>"abc",
"ta"=>"Describe yourself here");
$form = new HTML_QuickForm('register', 'post');
$form->addElement('text', 'firstName', 'Enter first name');
$form->addElement('password','password', 'Enter your password');
$form->addElement('textarea','ta','Description');
$form->addElement('submit','sb','Submit form');
$form->addRule('firstName','Your name is required', 'required');
if ($form->validate()) {
// processing code goes here.
$form->process('process_data', false);
}
else {
$form->setDefaults($defaults);
$form->display();
}
// For now just display the submitted vales, but you'll want to do a lot more
function process_data ($values) {
foreach ($values as $key=>$value) {
echo $key."=".$value."<br>";
}
}
?>
格式化表单
早期的HTML_QuickForm版本在改变layout的时候并没有那么灵活。但是,从3.0版本开始基于有 众所周知的Visitor设计样式。有8个renderer可用,而且有以下这些模板引擎直接支持:Smarty, HTML_Template_Sigma, HTML_Template_IT,HTML_Template_Flexy。要查看这些各式的renderer和模板引擎的细节必须在这篇文章之外,不过,在接下来的例子中简要地说明了HTML_QuickForm能够充分地定制你的表单输出。我的例子是凭空想象的,所以你一定要自己试试效果!确信无疑的是,如果你特别熟悉一个模板引擎,你会发现很快就能利用它。
<?
require_once "HTML/QuickForm.php";
$defaults_= array("firstName"=>"Ian",
"password"=>"abc",
"ta"=>"Describe yourself here");
$form = new HTML_QuickForm('register', 'post');
$form->addElement('text', 'firstName', 'Enter first name');
$form->addElement('password','password', 'Enter your password');
$form->addElement('textarea','ta','Description');
$form->addElement('submit','sb','Submit form');
$form->addRule('firstName','Your name is required', 'required');
$renderer =& $form->defaultRenderer();
$special_element_template='
<tr>
<td align="right" valign="top">
<!-- BEGIN required --><span style="color: magenta">*</span><!-- END required -->
<font size="+22">{label}</font>
</td>
<td valign="top" align="left">
<!-- BEGIN error --><span style="color: magenta">{error}</span><br /><!-- END error -->
{element}</td>
</tr>';
$renderer->setElementTemplate($special_element_template);
if ($form->validate()) {
// processing code goes here.
echo 'Success!';
}
else {
$form->setDefaults($defaults);
$form->display();
?>
结论
我确信你会发现它很容易使用,有关于这个包的更多的东西在官方的HTML_QuickForm(链接http://pear.php.net/manual/en/package.html.html-quickform.php)页面上,有API和更多的PEAR包,还有用户文档。而且不要忘记当HTML_QuickForm2发布的时候,去查看这个包关于PHP5东西。
原文地址:http://www.phpchina.com/?action_viewnews_itemid_2817.html
翻译/冰刺猬
验证是许多开发者的弱项,但是HTML_QuickForm使无论在客户端还是在服务器端的验证都非常的简单。它能做到这些是通过addRule()方式,之后,执行验证,用validate()方式。我们现在只能显示没有被验证的表单(因为在这个时候处理表单的代码能够被访问)。一切都很简单!这个例子有两个规则,一个是first name是必须填写的内容,第二个是first name必须只包含字母(并且不能是符号,所以就使得提交申请的人认真对待!):
<?
require_once "HTML/QuickForm.php";
$form = new HTML_QuickForm('register', 'post');
$form->addElement('text', 'firstName', 'Enter first name');
$form->addElement('password','password', 'Enter your password');
$form->addElement('textarea','ta','Description');
$form->addElement('submit','sb','Submit form');
$form->addRule('firstName', 'Your name is required', 'required');
$form->addRule('firstName', 'Your name can only contain letters','lettersonly');
if ($form->validate()) {
// processing code goes here.
echo 'Success!';
}
else {
$form->display();
}
?>
试着去看看当你用无效数据提交这个表单时会发生什么,然后用有效数据试一次,一个红色的*指出first name是需要填写的。如果数据被不正确地填写(左边有空格,或有除了字母以外的其他符号)一个适当的出错信息会用红色显示出来。
以上例子应用了required和lettersonly规则。以下列出了可被应用到的所有的规则:
rule argument description
alphanumeric Can only contain alphanumeric characters (letters and numbers).
compare Compares two values.
email true (which applies a DNS check) Must be a valid email (in syntax) (checks for a valid hostname if the argument is set to true).
filename $regex The filename of the uploaded file must match the regular expression in $regex.
lettersonly Can only contain letters.
maxfilesize $maxsize The filename of the uploaded file cannot be larger than $maxsize bytes.
maxlength $maxlength Can be at most $maxlength characters in length.
mimetype $mimetype MIME type of the uploaded file must either be of type $mimetype (if $mimetype is scalar), or match one of the elements in $mimetype (if it's an array).
minlength $minlength Must be at least $minlength in length.
numeric Must contain a valid integer or decimal number.
nonzero Cannot be zero.
nopunctuation Cannot contain any punctuation characters, which include: ( ) . / * ^ ? # ! @ $ % + = , " ' > < ~ [ ] { }.
rangelength $minlength,$maxlength Must be inclusively between $minlength and $maxlength characters in length.
regex $regex Must match the regular expression $regex.
required Cannot be blank
uploadedfile Must contain a successfully uploaded file.
让我们看看一些例子和特列。一个非常有用的特列是,你也可以在客户端验证,用"client"标记。在客户端进行验证是很好的做法,因为可使得用户的使用更加的简单(他们不需要等待服务器的应答),但是你通常还是需要在服务器端进行验证,因为并不是所有客户端都有JavaScript激活功能。我们同样着眼于comparison规则(在这个例子中两个空格不能一样),和一个custom规则,它允许你用你的想要的标准去写一个函数:
<?
require_once "HTML/QuickForm.php";
$form = new HTML_QuickForm('register', 'post');
$form->addElement('text', 'firstName', 'Enter first name');
$form->addElement('password','password', 'Enter your password');
$form->addElement('text','customer_email','Enter an email');
$form->addElement('textarea','ta','Description');
$form->addElement('submit','sb','Submit form');
$form->addRule('firstName', 'Your name is required', 'required');
$form->addRule('firstName', 'Your name can only contain letters','lettersonly');
$form->addRule('firstName', 'You must enter a first name', 'required', null, 'client');
$form->addRule('firstName', 'Your name cannot be less than two characters!','minlength',2,'client');
$form->addRule('customer_email', 'Please enter a valid email','email',true,'client');
$form->AddRule(array('password','firstName'),'Your password and first name cannot be the same!','compare','!=');
$form->registerRule('no_symbol','function','no_symbol_f');
$form->addRule('firstName','Sorry, your name cannot be Symbol','no_symbol');
if ($form->validate()) {
// processing code goes here.
echo 'Success!';
}
else {
$form->display();
}
function no_symbol_f($element_name,$element_value) {
if ($element_value == 'Symbol') {
return false;
}
else {
return true;
}
}
?>
以下是所要产生:
<script type="text/javascript">
//<![CDATA[
function validate_register(frm) {
var value = '';
var errFlag = new Array();
var _qfGroups = {};
_qfMsg = '';
value = frm.elements['firstName'].value;
if (value == '' && !errFlag['firstName']) {
errFlag['firstName'] = true;
_qfMsg = _qfMsg + '\n - You must enter a first name';
}
value = frm.elements['firstName'].value;
if (value != '' && value.length < 2 && !errFlag['firstName']) {
errFlag['firstName'] = true;
_qfMsg = _qfMsg + '\n - Your name cannot be less than two characters!';
}
value = frm.elements['customer_email'].value;
var regex = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;
if (value != '' && !regex.test(value) && !errFlag['customer_email']) {
errFlag['customer_email'] = true;
_qfMsg = _qfMsg + '\n - Please enter a valid email';
}
if (_qfMsg != '') {
_qfMsg = 'Invalid information entered.' + _qfMsg;
_qfMsg = _qfMsg + '\nPlease correct these fields.';
alert(_qfMsg);
return false;
}
return true;
}
//]]>
</script>
<form action="/phpbuilder/html_quickform33.php" method="post" name="register" id="register" >
<div>
<table border="0">
<tr>
<td align="right" valign="top"><span style="color: #ff0000">*</span><b>Enter first name</b></td>
<td valign="top" align="left"> <input name="firstName" type="text" /></td>
</tr>
<tr>
<td align="right" valign="top"><b>Enter your password</b></td>
<td valign="top" align="left"> <input name="password" type="password" /></td>
</tr>
<tr>
<td align="right" valign="top"><b>Enter an email</b></td>
<td valign="top" align="left"> <input name="customer_email" type="text" /></td>
</tr>
<tr>
<td align="right" valign="top"><b>Description</b></td>
<td valign="top" align="left"> <textarea name="ta"></textarea></td>
</tr>
<tr>
<td align="right" valign="top"><b></b></td>
<td valign="top" align="left"> <input name="sb" value="Submit form" type="submit" /></td>
</tr>
<tr>
<td></td>
<td align="left" valign="top"><span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span></td>
</tr>
</table>
</div>
</form>
在这个例子中,compare规则允许其他操作者使用非等(!=),或者<or>。如果你把操作者一起省去,它被假定为=,意思是这两个元素都必须相同(特别地适合去检查用户输入的密码或邮件地址是否正确)
处理表单数据
迄今为止,我们已经简单地成功显示了!如果表单已经被验证。虽然它看起来并不是特别的优秀。HTML_QuickForm 提供一个process()方式,它调用一个函数并审核提交值。以下是操作方式:
<?
require_once "HTML/QuickForm.php";
$defaults_= array("firstName"=>"Ian",
"password"=>"abc",
"ta"=>"Describe yourself here");
$form = new HTML_QuickForm('register', 'post');
$form->addElement('text', 'firstName', 'Enter first name');
$form->addElement('password','password', 'Enter your password');
$form->addElement('textarea','ta','Description');
$form->addElement('submit','sb','Submit form');
$form->addRule('firstName','Your name is required', 'required');
if ($form->validate()) {
// processing code goes here.
$form->process('process_data', false);
}
else {
$form->setDefaults($defaults);
$form->display();
}
// For now just display the submitted vales, but you'll want to do a lot more
function process_data ($values) {
foreach ($values as $key=>$value) {
echo $key."=".$value."<br>";
}
}
?>
格式化表单
早期的HTML_QuickForm版本在改变layout的时候并没有那么灵活。但是,从3.0版本开始基于有 众所周知的Visitor设计样式。有8个renderer可用,而且有以下这些模板引擎直接支持:Smarty, HTML_Template_Sigma, HTML_Template_IT,HTML_Template_Flexy。要查看这些各式的renderer和模板引擎的细节必须在这篇文章之外,不过,在接下来的例子中简要地说明了HTML_QuickForm能够充分地定制你的表单输出。我的例子是凭空想象的,所以你一定要自己试试效果!确信无疑的是,如果你特别熟悉一个模板引擎,你会发现很快就能利用它。
<?
require_once "HTML/QuickForm.php";
$defaults_= array("firstName"=>"Ian",
"password"=>"abc",
"ta"=>"Describe yourself here");
$form = new HTML_QuickForm('register', 'post');
$form->addElement('text', 'firstName', 'Enter first name');
$form->addElement('password','password', 'Enter your password');
$form->addElement('textarea','ta','Description');
$form->addElement('submit','sb','Submit form');
$form->addRule('firstName','Your name is required', 'required');
$renderer =& $form->defaultRenderer();
$special_element_template='
<tr>
<td align="right" valign="top">
<!-- BEGIN required --><span style="color: magenta">*</span><!-- END required -->
<font size="+22">{label}</font>
</td>
<td valign="top" align="left">
<!-- BEGIN error --><span style="color: magenta">{error}</span><br /><!-- END error -->
{element}</td>
</tr>';
$renderer->setElementTemplate($special_element_template);
if ($form->validate()) {
// processing code goes here.
echo 'Success!';
}
else {
$form->setDefaults($defaults);
$form->display();
?>
结论
我确信你会发现它很容易使用,有关于这个包的更多的东西在官方的HTML_QuickForm(链接http://pear.php.net/manual/en/package.html.html-quickform.php)页面上,有API和更多的PEAR包,还有用户文档。而且不要忘记当HTML_QuickForm2发布的时候,去查看这个包关于PHP5东西。
原文地址:http://www.phpchina.com/?action_viewnews_itemid_2817.html
翻译/冰刺猬
作者: songwl 发布时间: 2006-11-08
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28