關于ThinkPhp 框架表單驗證及ajax驗證問題
之前的表單驗證都是用js寫的,這里也可以使用tp框架的驗證。但是兩者比較而言還是js驗證比較好,因為tp框架驗證會運行后臺代碼,這樣運行速度和效率就會下降?!?/p>
自動驗證是ThinkPHP模型層提供的一種數據驗證方法,可以在使用create創(chuàng)建數據對象的時候自動進行數據驗證。驗證的代碼要寫在模型層即Model里面。
數據驗證有兩種方式:
靜態(tài)方式:在模型類里面通過$_validate屬性定義驗證規(guī)則。靜態(tài)方式定義好以后其它地方都可以使用。
動態(tài)方式:使用模型類的validate方法動態(tài)創(chuàng)建自動驗證規(guī)則。動態(tài)方式比較靈活,哪里使用就寫,其它地方不可以使用。
無論是什么方式,驗證規(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寫上對應的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'), ); }
二、動態(tài)驗證
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(//動態(tài)驗證就是需要在哪驗證就在哪里寫驗證方法。 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>
3.在thinkphp\Application\Home\Model里面寫模型文件。
<?php namespace Home\Model; use Think\Model; class YongHuModel extends Model { protected $tablePrefix = "";//表示表格前綴為空,就是沒有前綴。 protected $trueTableName = "yonghu";//如果不寫這句話,會自動去找Yong_Hu這張表,這是默認的表格的命名。這里要寫上實際的表格的名字。 }
三、Ajax做驗證
tp動態(tài)驗證和靜態(tài)驗證都有一個很大的缺點,那就是在提示錯誤信息的時候都要跳轉到其它頁面顯示出錯誤信息。如果需要在當前頁面顯示出錯誤信息,就需要用ajax做驗證。
1.寫顯示和ajax處理方法
<?php namespace Home\Controller; use Think\Controller; class TestController extends Controller { public function tianjia()//添加方法,用來顯示頁面 { $this->show(); } public function test()//ajax處理方法 { $y = D("YongHu"); $arr = array(//動態(tài)驗證就是需要在哪驗證就在哪里寫驗證方法。 array("uid","require","用戶名不能為空"),//講驗證的方法寫在方法里面 ); if($y->validate($arr)->create())//這里要先調用validate方法,然后將寫的驗證方法放到validate里面 { $this->ajaxReturn("通過驗證","eval"); } else { $this->ajaxReturn($y->getError(),"eval"); } } }
2.寫顯示頁面
<!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" /> <script src="__PUBLIC__/js/jquery-1.11.2.min.js"></script> <title>無標題文檔</title> <style type="text/css"> </style> </head> <body> <div>用戶名:<input id="uid" type="text" name="uid" /></div> <div><input id="btn" type="button" value="驗證" /></div> </body> <script type="text/javascript"> $("#btn").click(function(){ var uid = $("#uid").val(); $.ajax({ url:"__CONTROLLER__/test", data:{uid:uid}, type:"POST", dataType:"TEXT", success: function(data){ alert(data); } }) }) </script> </html>
總結
以上所述是小編給大家介紹的關于ThinkPhp 框架表單驗證及ajax,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
淺析echo(),print(),print_r(),return之間的區(qū)別
這篇文章主要是對echo(),print(),print_r(),return之間的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11