首页 php教程 PHP开发 yii2.0 模型rules验证详解

yii2.0 模型rules验证详解

Jan 03, 2017 am 09:42 AM

Yii2自带的注册可以作为网站的注册功能,但添加重复密码和验证码会更加完美!

问题:

用户名没有做严格的限制,类似“111”,“123456”,“_____111”这样的的用户名都是被允许的,那么如何限制用户输入我们所希望的用户名呢?

一般的注册,都有重复输入密码的input框,是为了让用户再次确认自己输入的密码,如何添加呢?

为了提高注册用户的质量,防止批量注册,添加验证码是不错的选择,如何加?

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

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

,
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => 'commonmodelsUser', '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' => 'commonmodelsUser', '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中添加[/b]

    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yiicaptchaCaptchaAction',
                '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' => 'commonmodelsUser', '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' => 'commonmodelsUser', 'message' => '该邮箱已经被注册!'],

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

['verifyCode', 'captcha'],
];
}
....
登录后复制

验证一下效果

827.png

以上就是yii2.0 模型rules验证详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)