如何在不修改逻辑代码的情况下完美解决以上三个问题?看了下面的教程,一目了然!

以高级版2.0.6为例,打开/frontend/models/SignupForm.php

class SignupForm extends Model
{
    public $username;
    public $email;
    public $password;     /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username', 'filter', 'filter' => 'trim'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
            ['username', 'string', 'min' => 2, 'max' => 255],             ['email', 'filter', 'filter' => 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],             ['password', 'required'],
            ['password', 'string', 'min' => 6],
        ];
    }

只需修改rules规则即可完美实现

a.添加用户字符限制,6-16位

['username', 'string', 'min' => 6, 'max' => 16],

输入限制:用户名由字母,汉字,数字,下划线组成,且不能以数字和下划线开头。

['username', 'match','pattern'=>'/^[(\x{4E00}-\x{9FA5})a-zA-Z]+[(\x{4E00}-\x{9FA5})a-zA-Z_\d]*$/u','message'=>'用户名由字母,汉字,数字,下划线组成,且不能以数字和下划线开头。'],

b.添加重复密码字段

public $repassword;

一般重复密码与密码的字段验证基本上是一致的,所以可以在password中添加repassword,并添加两次输入一致的限制

[['password','repassword'], 'required'],
[['password','repassword'], 'string', 'min' => 6],
['repassword', 'compare', 'compareAttribute' => 'password','message'=>'两次输入的密码不一致!'],

c.添加验证码字段

public $verifyCode;

验证码有自带的扩展,只需添加以下代码即可

['verifyCode', 'captcha'],

注意:需要在对应的控制器中添加以下代码,本例为SiteController中添加

    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

修改之后的规则

class SignupForm extends Model
{
    public $username;
    public $email;
    public $password;
    
    public $repassword;
    public $verifyCode;     public function rules()
    {
        return [
            ['username', 'filter', 'filter' => 'trim'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => '\common\models\User', 'message' => '该用户名已被使用!'],
            ['username', 'string', 'min' => 6, 'max' => 16],
            ['username', 'match','pattern'=>'/^[(\x{4E00}-\x{9FA5})a-zA-Z]+[(\x{4E00}-\x{9FA5})a-zA-Z_\d]*$/u','message'=>'用户名由字母,汉字,数字,下划线组成,且不能以数字和下划线开头。'],
            
            ['email', 'filter', 'filter' => 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => '该邮箱已经被注册!'],             [['password','repassword'], 'required'],
            [['password','repassword'], 'string', 'min' => 6],
            ['repassword', 'compare', 'compareAttribute' => 'password','message'=>'两次输入的密码不一致!'],
            
            ['verifyCode', 'captcha'],
        ];
    }
    
    ....

验证一下效果:

YII2.0中实现高级注册的更多相关文章

  1. yii2.0中url重写实现方法

    在yii框架里有前台和后台页面,举例前台url重写. 控制器与路由 控制器以Controller作为后缀,继承自yii\web\Controller; 动作以action作为前缀,public访问修饰 ...

  2. yii2.0 中的队列

    a yii2 extension to make simple to use queue. yii2-queue让队列的使用在yii2中变得更轻松,她为各种队列组件的使用提供了一个标准的接口,您只需要 ...

  3. 在Yii2.0中实现计划任务(cron)

    以下由我们在信易网络公司开发项目的时候终结出的一些经验 Create console application 创建命令行应用 In advance template there is already ...

  4. Yii2.0中场景的使用小记

    熟悉Yii框架的人都知道,灵活的使用场景可以达到事半功倍的效果! 比如普通的数据的新增.修改,新增需要验证其中两个字段,而修改只需要验证其中一个字段:还有种情况,也是我们现在用到的,同一张表(同一个m ...

  5. yii2.0中Rbac 怎么添加超加管理员

    最笨的是定义常量.具体怎么做?看下面: //定义在控制器声明上面define('BEST_PHPER',serialize(array('admin','admin1')));//设置admin管理员 ...

  6. yii2.0中数据缓存之增删改查

    public function actionSss(){ /* * 获取到缓存 * 这里是获取的是根目录下 的common/main.php中的缓存类组件 * */ $cache=\Yii::$app ...

  7. yii2.0中解决post的400错误

    不想用gii的表单自己写表单,但是又遇到了400错误,怎么解决?下面为你解答一下:

  8. 在yii2.0中封装一个生成验证码的控制器

    frontend目录下/封装的验证码类: <?php namespace frontend\controllers; use yii\base\Controller; class CapathC ...

  9. yii2.0中使用jquery

    我们都知道 yii 框架是组件式开发的,使用 jquery 也是非常简单的.只需要注册一下就可以使用非常简单的 jquery 代码了! <?php $this->beginBlock('s ...

随机推荐

  1. Merge Sorted Array [LeetCode]

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

  2. springMVC 验证器

    采用Hibernate-validator来进行验证,Hibernate-validator实现了JSR-303验证框架支持注解风格的验证.首先我们要到http://hibernate.org/val ...

  3. tableview调用reloadData()之后界面不刷新显示

    解决方法: 查看是否有指定tableView的delegate和datasource. self.tableView.delegate = self self.tableView.datasource ...

  4. Oracle后台进程

    后台进程简介 启动例程时,Oracle不仅会分配SGA,还会启动后台进程:关闭例程时,Oracle不仅会释放SGA所占用的内存空间,而且还会释放后台进程所占用的Cpu和内存资源.Oracle提供了很多 ...

  5. PHP的那些坑

    1.urlencode urlencode编码的对象必须是utf-8编码.如果是其它格式的编码就会出现乱码. 2.array_merge 一般来说,array_merge就是把两个或两个以上的数组组合 ...

  6. kafka客户端代码解析

    转载:http://backend.blog.163.com/blog/static/202294126201431724652597/ 可以使用服务器端下载的kafka二进制包及依赖,也可以通过ma ...

  7. js构建工具和预编译

    Gulp应该和Grunt比较,他们的区别我就不说了,说说用处吧.Gulp / Grunt 是一种工具,能够优化前端工作流程.比如自动刷新页面.combo.压缩css.js.编译less等等.简单来说, ...

  8. Qt 制作安装包

    Qt 制作在线.离线 安装包 见如下文档

  9. dancing link模板

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #i ...

  10. 警卫安排(dp好题)

    警卫安排(guard)[题目描述]一个重要的基地被分为 n 个连通的区域.出于某种神秘的原因,这些区域以一个区域为核心,呈一颗树形分布.在每个区域安排警卫所需要的费用是不同的,而每个区域的警卫都可以望 ...