The registration that comes with Yii2 can be used as the registration function of the website, but adding repeated passwords and verification codes will be more perfect!
Question:
There are no strict restrictions on user names. User names such as "111", "123456", and "_____111" are all allowed. So how to restrict user input? What about our desired username?
General registration, there is an input box for re-entering the password, which is to allow the user to confirm the password he entered again. How to add it?
In order to improve the quality of registered users and prevent batch registration, adding a verification code is a good choice. How to add it?
How to perfectly solve the above three problems without modifying the logic code? After reading the tutorial below, it’s clear at a glance!
Take the advanced version 2.0.6 as an example, open /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規(guī)則即可完美實現(xiàn) a.添加用戶字符限制,6-16位 ['username', 'string', 'min' => 6, 'max' => 16], 輸入限制:用戶名由字母,漢字,數(shù)字,下劃線組成,且不能以數(shù)字和下劃線開頭。 ['username', 'match','pattern'=>'/^[(x{4E00}-x{9FA5})a-zA-Z]+[(x{4E00}-x{9FA5})a-zA-Z_d]*$/u', 'message'=>'用戶名由字母,漢字,數(shù)字,下劃線組成,且不能以數(shù)字和下劃線開頭。'], 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, ], ]; }
The modified rules
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'=>'用戶名由字母,漢字,數(shù)字,下劃線組成,且不能以數(shù)字和下劃線開頭。'], ['email', 'filter', 'filter' => 'trim'], ['email', 'required'], ['email', 'email'], ['email', 'string', 'max' => 255], ['email', 'unique', 'targetClass' => 'commonmodelsUser', 'message' => '該郵箱已經(jīng)被注冊!'], [['password','repassword'], 'required'], [['password','repassword'], 'string', 'min' => 6], ['repassword', 'compare', 'compareAttribute' => 'password','message'=>'兩次輸入的密碼不一致!'], ['verifyCode', 'captcha'], ]; } ....
Verify the effect
The above is the detailed explanation of yii2.0 model rules verification. For more related content, please pay attention to the PHP Chinese website (www.miracleart.cn)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
