• thinkphp注册验证


    在model中新建一个UserModel

        //覆盖原本的设置
        //一次性获得全部验证错误
        protected $patchValidate    =   true;
        
        //实现表单项目验证
        //通过重写父类属性_validate实现表单验证
        protected $_validate        =   array(
            
            //验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
            //验证用户名,require必须填写项目
         //验证字段的名和表单传来的name需要一致 array('username','require','用户名必须填写'), array('password','require','密码必须填写'), //可以为同一个项目设置多个验证 array('password2','require','确认密码必须填写'), //与密码的值得是一致的 array('password2','password','两次密码不一致',0,'confirm'), //邮箱验证 array('user_email','email','邮箱格式不正确',2), //验证qq //都是数字的、长度5-10位、 首位不为0 //正则验证 /^[1-9]d{4,9}$/ array('user_qq',"/^[1-9]d{4,9}$/",'qq格式不正确'), //电话正则/^1[34578]d{9}$/ array('user_tel',"/^1[34578]d{9}$/",'电话格式不正确'), );

     在UserController.class.php中

    function register(){
            //因为有验证规则,所以需要用自定义的Model
            $user=new ModelUserModel;
            if(!empty($_POST)){
                //集成表单验证
                if(!$user->create()){
              $errorInfo=$user->getError();
              $this->assign('errorInfo',$errorInfo);
              $this->display(); }else{ $rst=$user->add(); if($rst){ $this->success('注册成功',U('Index/index')); //echo "success"; }else{ $this->success('注册失败',U('Index/index')); } } }else{ $this->display(); } }

     结果

    关于这个验证的用法,请看thinkphp手册

    http://document.thinkphp.cn/manual_3_2.html#auto_validate

  • 相关阅读:
    typescript
    js-解决安卓手机软键盘弹出后,固定定位布局被顶上移问题
    vue
    js
    Object.assgin基本知识与相关深浅拷贝
    js-工具方法(持续更新)
    vue
    vue
    git
    css
  • 原文地址:https://www.cnblogs.com/anxiaoyu/p/6903863.html
Copyright © 2020-2023  润新知