首页 > 代码库 > thinkphp注册验证
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 \Model\UserModel; 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
thinkphp注册验证
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。