TP框架---thinkphp表单验证

自动验证是ThinkPHP模型层提供的一种数据验证方法,能够在使用create建立数据对象的时候自动进行数据验证。验证的代码要写在模型层即Model里面。javascript

  数据验证有两种方式:php

  1. 静态方式:在模型类里面经过$_validate属性定义验证规则。静态方式定义好之后其它地方均可以使用。
  2. 动态方式:使用模型类的validate方法动态建立自动验证规则。动态方式比较灵活,哪里使用就写,其它地方不可使用。

不管是什么方式,验证规则的定义是统一的规则,定义格式为:css

复制代码
array(//验证规则是一个二维数组,里面是觉得数组,每一个一维数组是一个验证。括号里面的信息都是须要写的。 array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),//1.验证字段(必须的):须要验证哪一个字段;2.验证规则(必须的):使用什么规则验证;3.错误提示(必须的):验证失败后提示的信息;4.验证条件(可选的):用什么条件来验证,是可选的条件;5.附加规则(可选的):附加的一些规则;6.验证时间(可选的):好比往数据库里写的时候验证,修改的时候验证; array(验证字段2,验证规则,错误提示,[验证条件,附加规则,验证时间]), ...... );
复制代码

 

1、静态验证html

1.先在Application\Home\Controller里面写方法java

复制代码
<?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");//造YongHu表的对象 $r = $y->create();//自动收集表单,收集表单的时候自动验证。 if($r)//验证经过返回true  { $y->add();//验证经过后将收集的表单加到表格里面。  } else//若是验证失败  { die($y->getError());//表示输出错误信息而且退出程序  } } } }
复制代码

2.在thinkphp\Application\Home\View\Test写上对应的html文件jquery

复制代码
<!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里面写模型文件,也就是验证的方法。ajax

复制代码
<?php
namespace Home\Model;
use Think\Model; class YongHuModel extends Model { protected $tablePrefix = "";//表示表格前缀为空,就是没有前缀。 protected $trueTableName = "yonghu";//若是不写这句话,会自动去找Yong_Hu这张表,这是默认的表格的命名。这里要写上实际的表格的名字。 protected $_validate = array( array("uid","require","用户名不能为空",0),//对用户名加非空验证。手册里面没有非空的验证,本身写。1.字段名为uid;2.require表示字段必须,写在这里意思就是不能为空;3.提示信息;4.self::EXISTS_VALIDATE 或者0 存在字段就验证(默认),这里写0表明的就是存在字段就去验证。 array("pwd",'pwd1',"两次输入的密码不一致",0,"confirm"), //验证2此输入的密码是否相同.confirm表示验证表单中的两个字段是否相同,定义的验证规则是一个字段名,因此第二个字段名写第2此输入密码的密码框的name值pwd1 array("email","email","邮箱格式输入不正确"),//验证邮箱书写是否正确。email是tp框架里面自带的验证方法,表明的是邮箱验证。 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"),//身份证号验证。regex表示正则表达式,第2项验证规则里面就须要写上身份证号的正则表达式。 array("age","18,50","年龄不在范围内",0,"between"),//范围验证,用in、between均可以,这里使用between。若是在输入信息的时候出现大于1个错误,会按照这里的顺序输出第一个错误信息。  ); }
复制代码

 

2、动态验证正则表达式

1.在Application\Home\Controller里面写方法thinkphp

复制代码
<?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>
复制代码

3.在thinkphp\Application\Home\Model里面写模型文件。

复制代码
<?php
namespace Home\Model;
use Think\Model; class YongHuModel extends Model { protected $tablePrefix = "";//表示表格前缀为空,就是没有前缀。 protected $trueTableName = "yonghu";//若是不写这句话,会自动去找Yong_Hu这张表,这是默认的表格的命名。这里要写上实际的表格的名字。 }
复制代码

 

3、Ajax作验证

tp动态验证和静态验证都有一个很大的缺点,那就是在提示错误信息的时候都要跳转到其它页面显示出错误信息。若是须要在当前页面显示出错误信息,就须要用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(//动态验证就是须要在哪验证就在哪里写验证方法。 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>
复制代码

3.显示页面

相关文章
相关标签/搜索