之前的表單驗證都是用js寫的,這里也可以使用tp框架的驗證。但是兩者比較而言還是js驗證比較好,因為tp框架驗證會運行后臺代碼,這樣運行速度和效率就會下降。
自動驗證是ThinkPHP模型層提供的一種數據驗證方法,可以在使用create創建數據對象的時候自動進行數據驗證。驗證的代碼要寫在模型層即Model里面。
數據驗證有兩種方式:
靜態方式:在模型類里面通過$_validate屬性定義驗證規則。靜態方式定義好以后其它地方都可以使用。
動態方式:使用模型類的validate方法動態創建自動驗證規則。動態方式比較靈活,哪里使用就寫,其它地方不可以使用。
無論是什么方式,驗證規則的定義是統一的規則,定義格式為:
<?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寫上對應的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>無標題文檔</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>確認密碼:<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里面寫模型文件,也就是驗證的方法。
<?php namespace Home\Model; use Think\Model; class YongHuuModel extends Model { protected $tablePrefix = ""; protected $trueTableName = 'yonghuu'; //真實表名 //protected $patchValidate = true; protected $_validate = array( array('uid','require','用戶名不能為空!'), array('pwd','pwd1','兩次輸入的密碼不一致!',0,'confirm'), //兩個字段是否相同 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)$/','身份證號不正確!',0,'regex'), array('age','18,50','年齡不在范圍內',0,'between'), ); }
二、動態驗證
1.在Application\Home\Controller里面寫方法
<?php namespace Home\Controller; use Think\Controller; class TestController extends Controller { public function add() { if(empty($_POST))//如果post數組為空 { $this->show();//顯示add.html頁面 } else//如果post數組不為空 { $y = D("YongHu"); $arr = array(//動態驗證就是需要在哪驗證就在哪里寫驗證方法。 array("uid","require","用戶名不能為空",0),//講驗證的方法寫在方法里面 ); if($y->validate($arr)->create())//這里要先調用validate方法,然后將寫的驗證方法放到validate里面 { $y->add(); } else { die($y->getError()); } } } }
2.在thinkphp\Application\Home\View\Test寫上對應的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>無標題文檔</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>確認密碼:<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>
【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。