关于ThinkPhp 框架表单验证及ajax验证问题

时间:2019-03-30
本文章向大家介绍关于ThinkPhp 框架表单验证及ajax验证问题,主要包括关于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">
<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>

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>
    <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,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!