您的位置:首页 > 编程语言 > PHP开发

6月17日 TP表单验证

2016-06-21 10:08 495 查看
用做验证登录格式

ZhuCeController.class.php:

<?php
namespace Home\Controller;
use Think\Controller;
class ZhuCeController extends Controller
{
//静态验证
function ZhuCe()
{
if(empty($_POST))
{
$this->display();
}
else
{
$model = new \Home\Model\testModel();
if(!$model->create())
{
exit($model->getError());
}
else
{
$model->add();
}
}
}

//动态验证
function YanZheng()
{
$cw = "";
if(!empty($_GET))
{
$cw = $_GET["cw"];
}
if(empty($_POST))
{
$this->assign("error",$cw);
$this->display();
}
else
{
$mmodel = new \Home\Model\testModel();
$rules = array(

array('uid','require','用户名不能为空'),
array('pwd','require','密码不能为空'),
array('pwd','pwd1','两次输入的密码不一致',1,'confirm'),
array('age','18,50','年龄必须在18到50之间',1,'between'),
array('email','email','邮箱格式输入不正确'),

);

if(!$model->validate($rules)->create())
{
echo $model->getError();
}

}
}

}


静态验证:

ZhuCe.html:

<body>
<h1>注册页面</h1>
<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="age" /></div>
<div>邮箱:<input type="text" name="email" /></div>
<div>姓名:<input type="text" name="name" /></div>
<input type="submit" value="注册" />
</form>
</body>


testModel.class.php:

<?php
namespace Home\Model;
use Think\Model;
class testModel extends Model
{
protected $_validate = array(
array('uid','require','用户名不能为空'),
array('pwd','require','密码不能为空'),
array('pwd','pwd1','两次输入的密码不一致',1,'confirm'),
array('age','18,50','年龄必须在18到50之间',1,'between'),
array('email','email','邮箱格式输入不正确'),
);
}


动态验证:

ZhuCeController.class.php:

<?php
namespace Home\Controller;
use Think\Controller;
class ZhuCeController extends Controller
{

//动态验证
function YanZheng()
{
$cw = "";
if(!empty($_GET))
{
$cw = $_GET["cw"];
}
if(empty($_POST))
{
$this->assign("error",$cw);
$this->display();
}
else
{
$mmodel = new \Home\Model\testModel();
$rules = array(

array('uid','require','用户名不能为空'),
array('pwd','require','密码不能为空'),
array('pwd','pwd1','两次输入的密码不一致',1,'confirm'),
array('age','18,50','年龄必须在18到50之间',1,'between'),
array('email','email','邮箱格式输入不正确'),

);

if(!$model->validate($rules)->create())
{
echo $model->getError();
}

}
}

}


YanZheng.html:

<body>
<h1>注册页面</h1>
<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="age" /></div>
<div>邮箱:<input type="text" name="email" /></div>
<div>姓名:<input type="text" name="name" /></div>
<div><{$error}></div>
<input type="submit" value="注册" />
</form>
</body>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: