Home >Backend Development >PHP Tutorial >Thinkphp添加自动验证规则后无法写入数据库也没有任何报错信息,怎么回事?

Thinkphp添加自动验证规则后无法写入数据库也没有任何报错信息,怎么回事?

WBOY
WBOYOriginal
2016-06-06 20:21:211447browse

只要添加UserModel.class.php就不能提交数据到数据库,而且没有任何提示,表单页面也没有任何提交成功或者失败的提示。
代码如下:(Thinkphp 3.2)
接收表单的控制器的代码:

<code><?php namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
     public function index(){
         $this->display();
         
        $user=D("User");//自动验证并没有实现,为什么?
        if($user->create()){
            if($user->add()){$this->success("注册成功,页面正在跳转...");}
            else{$user->getError();}
        }else{$user->getError();}
        }    
     } </code>

UserModel.class.php中的代码 :

<code><?php namespace Home\Model;
use Think\Model;
class UserModel extends Model{
    protected $_map=array(
          "pwd2"=>"psd",//数据库中用的是psd字段
        );
    protected $_validate = array(
         //array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
         array("uname","require","用户名不能为空",1),
         array("uname","/^\w{3,16}$/","用户名必须由3-16位的数字字母下划线组成",1,"regex"),
         array("uname","","用户名已经存在",1,"unique"),
         array("pwd1","require","密码不能为空"),
         array('pwd1', '/^\w{6,20}$/s',"密码格式不正确",1,"regex"),
         array('pwd2', 'password', '两次密码不一致', 1, 'confirm'),
         array("email","email","邮箱格式不正确性",1),
         array("email","","邮箱已注册,请换一个邮箱","unique")
        );

    //自动完成
    protected $_auto = array(
        array('pwd2', 'md5', 3, 'function')
        );

}</code>

回复内容:

只要添加UserModel.class.php就不能提交数据到数据库,而且没有任何提示,表单页面也没有任何提交成功或者失败的提示。
代码如下:(Thinkphp 3.2)
接收表单的控制器的代码:

<code><?php namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{
     public function index(){
         $this->display();
         
        $user=D("User");//自动验证并没有实现,为什么?
        if($user->create()){
            if($user->add()){$this->success("注册成功,页面正在跳转...");}
            else{$user->getError();}
        }else{$user->getError();}
        }    
     } </code>

UserModel.class.php中的代码 :

<code><?php namespace Home\Model;
use Think\Model;
class UserModel extends Model{
    protected $_map=array(
          "pwd2"=>"psd",//数据库中用的是psd字段
        );
    protected $_validate = array(
         //array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
         array("uname","require","用户名不能为空",1),
         array("uname","/^\w{3,16}$/","用户名必须由3-16位的数字字母下划线组成",1,"regex"),
         array("uname","","用户名已经存在",1,"unique"),
         array("pwd1","require","密码不能为空"),
         array('pwd1', '/^\w{6,20}$/s',"密码格式不正确",1,"regex"),
         array('pwd2', 'password', '两次密码不一致', 1, 'confirm'),
         array("email","email","邮箱格式不正确性",1),
         array("email","","邮箱已注册,请换一个邮箱","unique")
        );

    //自动完成
    protected $_auto = array(
        array('pwd2', 'md5', 3, 'function')
        );

}</code>

<code> $user=D("user");//自动验证并没有实现,为什么? </code>

D("User") 试试 ,注意D方法实例化的时候的模型名称的大小写.

与数据库中的大小写要一致哦

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn