Rumah > pembangunan bahagian belakang > tutorial php > 关于ThinkPhp框架表单验证和ajax验证问题的分析

关于ThinkPhp框架表单验证和ajax验证问题的分析

不言
Lepaskan: 2023-03-30 19:30:01
asal
1370 orang telah melayarinya

这篇文章主要介绍了关于ThinkPhp框架表单验证和ajax验证问题的分析,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

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());
}
}
} 
}
Salin selepas log masuk

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>
Salin selepas log masuk

3.在thinkphp\Application\Home\Model里面写模型文件,也就是验证的方法。

<?php
namespace Home\Model;
use Think\Model;
class YongHuuModel extends Model
{
protected $tablePrefix = "";
protected $trueTableName = &#39;yonghuu&#39;; //真实表名
//protected $patchValidate = true;
protected $_validate = array(
array(&#39;uid&#39;,&#39;require&#39;,&#39;用户名不能为空!&#39;),
array(&#39;pwd&#39;,&#39;pwd1&#39;,&#39;两次输入的密码不一致!&#39;,0,&#39;confirm&#39;), //两个字段是否相同
array(&#39;email&#39;,&#39;email&#39;,&#39;邮箱格式不正确&#39;),
array(&#39;name&#39;,&#39;/^[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)$/&#39;,&#39;身份证号不正确!&#39;,0,&#39;regex&#39;),
array(&#39;age&#39;,&#39;18,50&#39;,&#39;年龄不在范围内&#39;,0,&#39;between&#39;),
);
}
Salin selepas log masuk

二、动态验证

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());
      }
    }
  }
}
Salin selepas log masuk

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>
Salin selepas log masuk

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

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

三、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");
      }
  }
}
Salin selepas log masuk

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>
Salin selepas log masuk

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

ThinkPHP3.2框架使用addAll()批量插入数据的方法

thinkPHP5中ajax提交表单操作

thinkphp和redis以及队列结合实现的代码

Atas ialah kandungan terperinci 关于ThinkPhp框架表单验证和ajax验证问题的分析. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan