静态验证

   //D:\LearnWebDevelop\php\thinkphp_3.2.3_full\Application\OuterMok2\Controller\UserController.class.php 
    public function register(){    
        if (IS_POST){
            header("Content-type: text/html; charset=utf-8");
            var_dump($_POST);
            $User = D("UserRegister"); // 实例化User对象
            if (!$User->create()){
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                exit($User->getError());
            }else{
                // 验证通过 可以进行其他数据操作
                $User->add();
                echo '<hr>add user OK!';
            }

        }else{
            $this->display();
        }
        
    }

 

D:\LearnWebDevelop\php\thinkphp_3.2.3_full\Application\OuterMok2\View\User\register.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta charset="UTF-8">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>

<form method="post" action="register">
<input type="text" name="name" >
<input type="text" name="email" >
<input type="password" name="password" >
<input type="password" name="repassword" >


<input type="submit">
</form>

 </body>
</html>

 

<?php
//D:\LearnWebDevelop\php\thinkphp_3.2.3_full\Application\OuterMok2\Model\UserRegisterModel.class.php
namespace OuterMok2\Model;
use Think\Model;
class UserRegisterModel extends Model{
    
protected $_validate = array(
array('name','require','用户名必须!'), // 用户名必须
array('name','','帐号名称已经存在!',1,'unique',1), // 验证用户名是否已经存在
array('email','email','Email格式错误!',2), // 如果输入则验证Email格式是否正确
array('password','6,30','密码长度不正确',0,'length'), // 验证密码是否在指定长度范围
array('repassword','password','确认密码不一致',0,'confirm'), // 验证确认密码是否和密码一致
);

}

 

  //D:\LearnWebDevelop\php\thinkphp_3.2.3_full\Application\OuterMok2\Controller\UserController.class.php 
    public function register(){    
        if (IS_POST){
            header("Content-type: text/html; charset=utf-8");
            var_dump($_POST);
            
            /*
            $rules = array(
                array('name','require','用户名必须!'), // 用户名必须
                array('name','','帐号名称已经存在!',1,'unique',1), // 验证用户名是否已经存在
                array('email','email','Email格式错误!',2), // 如果输入则验证Email格式是否正确
                array('password','6,30','密码长度不正确',0,'length'), // 验证密码是否在指定长度范围
                array('repassword','password','确认密码不一致',0,'confirm'), // 验证确认密码是否和密码一致
            );
            $User = M("UserRegister"); // 实例化User对象
            //$User = D("UserRegister"); 
            if (!$User->validate($rules)->create()){
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                exit($User->getError());
            }else{
                // 验证通过 可以进行其他数据操作
                $User->add();
                echo '<hr>add user OK!';
            }
            */
            
            //$User = M("UserRegister"); // 实例化User对象
            $User = D("UserRegister"); // 实例化User对象
            if (!$User->create()){
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                exit($User->getError());
            }else{
                // 验证通过 可以进行其他数据操作
                $User->add();
                echo '<hr>add user OK!';
            }
            

        }else{
            $this->display();
        }
        
    }

 

posted @ 2017-12-18 15:22  sky20080101  阅读(69)  评论(0)    收藏  举报