6月17日 TP表单验证

用做验证登录格式

ZhuCeController.class.php:

<?php
namespace HomeController;
use ThinkController;
class ZhuCeController extends Controller
{
    //静态验证
    function ZhuCe()
    {
        if(empty($_POST))
        {
            $this->display();
        }
        else
        {
            $model = new HomeModel	estModel();
            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 HomeModel	estModel();
        $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 HomeModel;
use ThinkModel;
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 HomeController;
use ThinkController;
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 HomeModel	estModel();
        $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>
原文地址:https://www.cnblogs.com/dongqiaozhi/p/5602681.html