自动验证是thinkphp模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证。分为静态验证和动态验证。本文主要和大家介绍thinkphp框架表单验证操作方法,需要的朋友可以参考下,希望能帮助到大家。
一、静态验证
(1)在home/controller/路径下新建index控制器。indexcontroller
indexcontroller.class.php页面
注意:静态定义方式因为必须定义模型类,所以只能用d函数实例化模型
create方法是对表单提交的post数据进行自动验证
<?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页面
<!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">
<p>用户名:<input type="text" name="uid" /></p>
<p>密码:<input type="password" name="pwd1"/></p>
<p>确认密码:<input type="password" name="pwd2"/></p>
<p>年龄:<input type="text" name="age"/></p>
<p>邮箱:<input type="text" name="email"/></p>
<p><input type="submit" value="验证" /></p>
</form>
</body>
</html>
效果图:
(3)在model层写静态验证的验证:(路径如图)
usersmodel.class.php
<?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","邮箱格式不正确"),
);
}
依次验证效果图:
当全部为空时,点击验证
会跳转
输入用户名,其他不输入时,会跳转
两次密码输入不一致时,会提示;年龄不在范围内会提示;邮箱格式不正确时会提示;
输入正确格式内容后
二、动态验证
(1) indexcontroller.class.php页面
<?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页面:
<!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">
<p><input type="text" name="uid" id="uid" /><span id="ts"></span></p>
<p><input type="submit" value="验证" /></p>
</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框架实现表单验证功能代码的详细内容。
