服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - PHP教程 - ThinkPHP框架表单验证操作方法

ThinkPHP框架表单验证操作方法

2021-06-06 19:24ChrissZhao PHP教程

这篇文章主要介绍了ThinkPHP框架表单验证操作方法,需要的朋友可以参考下

自动验证是thinkphp模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证。分为静态验证和动态验证。

一、静态验证

(1)在home/controller/路径下新建index控制器。indexcontroller

ThinkPHP框架表单验证操作方法

 indexcontroller.class.php页面

注意:静态定义方式因为必须定义模型类,所以只能用d函数实例化模型

     create方法是对表单提交的post数据进行自动验证

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
namespace home\controller;
use think\controller;
class indexcontroller extends controller {
    public function yanzheng(){
    $u= d("users");//造一个子类对象
    if(empty($_post)){
      $this->show();
    }else{
      if($u->create()){//验证
        echo"验证通过";
      }else{
        echo $u->geterror();//获取错误信息
      }
    }
  }
}

 (2)在view/index文件夹下做yanzheng.html页面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!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>
<script src="__root__/public/js/jquery-3.2.0.min.js"></script>
</head>
<body>
<h1>验证界面</h1>
<form action="__action__" method="post">
<div>用户名:<input type="text" name="uid" /></div>
<div>密码:<input type="password" name="pwd1"/></div>
<div>确认密码:<input type="password" name="pwd2"/></div>
<div>年龄:<input type="text" name="age"/></div>
<div>邮箱:<input type="text" name="email"/></div>
<div><input type="submit" value="验证" /></div>
</form>
</body>
</html>

 效果图:

ThinkPHP框架表单验证操作方法

(3)在model层写静态验证的验证:(路径如图)

ThinkPHP框架表单验证操作方法

usersmodel.class.php

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
namespace home\model;
use think\model;
class usersmodel extends model{
    //添加验证条件
    protected $_validate = array
      array("uid","require","用户名不能为空!"), //默认情况下用正则进行验证
      array("pwd1","require","密码不能为空!"),
      array("pwd2","require","密码不能为空!"),  
      array("pwd2","pwd1","两次输入的密码不一致",0,"confirm"), // 验证确认密码是否和密码一致
      array("age","18,50","年龄不在范围内",0,"between"),
      array("email","email","邮箱格式不正确"),
    );
}

 依次验证效果图:

当全部为空时,点击验证

ThinkPHP框架表单验证操作方法

会跳转

输入用户名,其他不输入时,会跳转

两次密码输入不一致时,会提示;年龄不在范围内会提示;邮箱格式不正确时会提示;

ThinkPHP框架表单验证操作方法 ThinkPHP框架表单验证操作方法

ThinkPHP框架表单验证操作方法

输入正确格式内容后

 二、动态验证

(1)  indexcontroller.class.php页面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
namespace home\controller;
use think\controller;
class indexcontroller extends controller {
    public function yz(){
    $u= m("users");//造一个父类对象
    if(empty($_post)){
      $this->show();
    }else{
      $rules = array(
        array("uid","require","用户名不能为空!"),
      );
      if($u->validate($rules)->create()){//验证
        $this->ajaxreturn("ok","eval");
      }else{
        $this->ajaxreturn("no","eval");
      }
    }
  }

 (2) yz.html页面:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!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>
<script src="__root__/public/js/jquery-3.2.0.min.js"></script>
</head>
<body>
<h1>验证界面</h1>
<form action="__action__" method="post">
<div><input type="text" name="uid" id="uid" /><span id="ts"></span></div>
<div><input type="submit" value="验证" /></div>
</form>
</body>
<script type="text/javascript">
$("#uid").blur(function(){
    var uid = $(this).val();
    $.ajax({
        url:"__action__",
        data:{uid:uid},
        type:"post",
        datatype:"text",
        success: function(data){
            if(data.trim()=="ok")
            {
              $("#ts").html("验证通过");
            }
            else
            {
              $("#ts").html("用户名不能为空");
            }
          }
      });
  })
</script>
</html>

 看一下效果:

ThinkPHP框架表单验证操作方法

 当文本框失去焦点时:

ThinkPHP框架表单验证操作方法

当文本框有内容时,再失去焦点:

ThinkPHP框架表单验证操作方法

以上所述是小编给大家介绍的thinkphp框架表单验证操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://www.cnblogs.com/zhaohui123/p/7154658.html

延伸 · 阅读

精彩推荐