tp数据验证有两种方式,一种是静态方式,一种是动态方式,下面小编给大家带来了thinkphp 框架表单验证及ajax验证问题,感兴趣的朋友一起看看吧
之前的表单验证都是用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">
<p>用户名:<input type="text" name="uid" /></p>
<p>密码:<input type="text" name="pwd" /></p>
<p>确认密码:<input type="text" name="pwd1" /></p>
<p>姓名:<input type="text" name="name" /></p>
<p>邮箱:<input type="text" name="email" /></p>
<p>年龄:<input type="text" name="age" /></p>
<p><input type="submit" value="提交" /></p>
</form>
</p>
</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">
<p>用户名:<input type="text" name="uid" /></p>
<p>密码:<input type="text" name="pwd" /></p>
<p>确认密码:<input type="text" name="pwd1" /></p>
<p>姓名:<input type="text" name="name" /></p>
<p>邮箱:<input type="text" name="email" /></p>
<p>年龄:<input type="text" name="age" /></p>
<p><input type="submit" value="提交" /></p>
</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动态验证和静态验证都有一个很大的缺点,那就是在提示错误信息的时候都要跳转到其它页面显示出错误信息。如果需要在当前页面显示出错误信息,就需要用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>
<p>用户名:<input id="uid" type="text" name="uid" /></p>
<p><input id="btn" type="button" value="验证" /></p>
</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验证详解的详细内容。