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规则即可完美实现 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, ], ]; }
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'=>'用户名由字母,汉字,数字,下划线组成,且不能以数字和下划线开头。'], ['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'], ]; } ....
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.php.cn)!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment