搜尋
首頁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'],
];
}
....

驗證一下效果

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尊渡假赌尊渡假赌尊渡假赌

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能