之前的表單驗(yàn)證都是用js寫(xiě)的,這里也可以使用tp框架的驗(yàn)證。但是兩者比較而言還是js驗(yàn)證比較好,因?yàn)閠p框架驗(yàn)證會(huì)運(yùn)行后臺(tái)代碼,這樣運(yùn)行速度和效率就會(huì)下降。
自動(dòng)驗(yàn)證是ThinkPHP模型層提供的一種數(shù)據(jù)驗(yàn)證方法,可以在使用create創(chuàng)建數(shù)據(jù)對(duì)象的時(shí)候自動(dòng)進(jìn)行數(shù)據(jù)驗(yàn)證。驗(yàn)證的代碼要寫(xiě)在模型層即Model里面。
數(shù)據(jù)驗(yàn)證有兩種方式:
靜態(tài)方式:在模型類里面通過(guò)$_validate屬性定義驗(yàn)證規(guī)則。靜態(tài)方式定義好以后其它地方都可以使用。
動(dòng)態(tài)方式:使用模型類的validate方法動(dòng)態(tài)創(chuàng)建自動(dòng)驗(yàn)證規(guī)則。動(dòng)態(tài)方式比較靈活,哪里使用就寫(xiě),其它地方不可以使用。
無(wú)論是什么方式,驗(yàn)證規(guī)則的定義是統(tǒng)一的規(guī)則,定義格式為:
<?php namespace Home\Controller; use Think\Controller; class TestController extends Controller { public function add() { if(empty($_POST)) { $this->show(); } else { $y=new \Home\Model\YongHuuModel(); $r=$y->create(); if($r) { $y->add(); } else{ die($y->getError()); } } } }
2.在thinkphp\Application\Home\View\Test寫(xiě)上對(duì)應(yīng)的html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無(wú)標(biāo)題文檔</title> </head> <style type="text/css"> *{ font-family:微軟雅黑; padding:0px; margin:0px auto} </style> <body> <form action="__ACTION__" method="post"> <div>用戶名:<input type="text" name="uid" /></div> <div>密碼:<input type="text" name="pwd" /></div> <div>確認(rèn)密碼:<input type="text" name="pwd1" /></div> <div>姓名:<input type="text" name="name" /></div> <div>郵箱:<input type="text" name="email" /></div> <div>年齡:<input type="text" name="age" /></div> <div><input type="submit" value="提交" /></div> </form> </div> </body> </html>
3.在thinkphp\Application\Home\Model里面寫(xiě)模型文件,也就是驗(yàn)證的方法。
<?php namespace Home\Model; use Think\Model; class YongHuuModel extends Model { protected $tablePrefix = ""; protected $trueTableName = 'yonghuu'; //真實(shí)表名 //protected $patchValidate = true; protected $_validate = array( array('uid','require','用戶名不能為空!'), array('pwd','pwd1','兩次輸入的密碼不一致!',0,'confirm'), //兩個(gè)字段是否相同 array('email','email','郵箱格式不正確'), array('name','/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/','身份證號(hào)不正確!',0,'regex'), array('age','18,50','年齡不在范圍內(nèi)',0,'between'), ); }
二、動(dòng)態(tài)驗(yàn)證
1.在Application\Home\Controller里面寫(xiě)方法
<?php namespace Home\Controller; use Think\Controller; class TestController extends Controller { public function add() { if(empty($_POST))//如果post數(shù)組為空 { $this->show();//顯示add.html頁(yè)面 } else//如果post數(shù)組不為空 { $y = D("YongHu"); $arr = array(//動(dòng)態(tài)驗(yàn)證就是需要在哪驗(yàn)證就在哪里寫(xiě)驗(yàn)證方法。 array("uid","require","用戶名不能為空",0),//講驗(yàn)證的方法寫(xiě)在方法里面 ); if($y->validate($arr)->create())//這里要先調(diào)用validate方法,然后將寫(xiě)的驗(yàn)證方法放到validate里面 { $y->add(); } else { die($y->getError()); } } } }
2.在thinkphp\Application\Home\View\Test寫(xiě)上對(duì)應(yīng)的html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無(wú)標(biāo)題文檔</title> <style type="text/css"> </style> </head> <body> <form action="__ACTION__" method="post"> <div>用戶名:<input type="text" name="uid" /></div> <div>密碼:<input type="text" name="pwd" /></div> <div>確認(rèn)密碼:<input type="text" name="pwd1" /></div> <div>姓名:<input type="text" name="name" /></div> <div>郵箱:<input type="text" name="email" /></div> <div>年齡:<input type="text" name="age" /></div> <div><input type="submit" value="提交" /></div> </form> </body> <script type="text/javascript"> </script> </html>