Home > Backend Development > PHP Tutorial > Detailed introduction of Yii2.0 rules verification rule set

Detailed introduction of Yii2.0 rules verification rule set

黄舟
Release: 2023-03-06 20:00:01
Original
1574 people have browsed it

This article mainly introduces the detailed explanation of Yii2.0 rulesVerification rulesset, which has certain reference value. Interested friends can refer to it.

I am also on the way to learn Yii2 recently, so today can be considered as a study note!

required: Must value verification attribute

The code is as follows:

[['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; 
#说明:CRequiredValidator 的别名, 确保了特性不为空.
Copy after login

email : Email verification

The code is as follows:

['email', 'email']; #说明:CEmailValidator的别名,确保了特性的值是一个有效的电邮地址.
Copy after login

match: regular verification

[['字段名'],match,'pattern'=>'正则表达式','message'=>'提示信息']; 

[['字段名'],match,'not'=>ture,'pattern'=>'正则表达式','message'=>'提示信息']; 
/*正则取反*/ #说明:CRegularExpressionValidator 的别名, 确保了特性匹配一个正则表达式.
Copy after login

url: URL

The code is as follows:

['website', 'url', 'defaultScheme' => 'http']; #说明:CUrlValidator 的别名, 确保了特性是一个有效的路径.
Copy after login

captcha: Verification code

The code is as follows:

['verificationCode', 'captcha']; #说明:CCaptchaValidator 的别名,确保了特性的值等于 CAPTCHA 显示出来的验证码.
Copy after login

safe : Safe

['description', 'safe'];
Copy after login

compare : Compare the

code as follows:

['age', 'compare', 'compareValue' => 30, 'operator' => '>=']; 
#说明:compareValue(比较常量值) - operator(比较操作符)  
#说明:CCompareValidator 的别名,确保了特性的值等于另一个特性或常量.
Copy after login

default : Default value

The code is as follows:

['age', 'default', 'value' => null]; #说明:CDefaultValueValidator 的别名, 为特性指派了一个默认值.
Copy after login

exist: Existence

The code is as follows:

['username', 'exist']; #说明:CExistValidator 的别名,确保属性值存在于指定的数据表字段中.
Copy after login

file: File

The code is as follows:

['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024*1024]; 
#说明:CFileValidator 的别名, 确保了特性包含了一个上传文件的名称.
Copy after login

filter: filter

The code is as follows:

[['username', 'email'], 'filter', 'filter' => 'trim', 'skipOnArray' => true]; 
#说明:CFilterValidator 的别名, 使用一个filter转换属性.
Copy after login

in: Range

The code is as follows:

['level', 'in', 'range' => [1, 2, 3]]; #说明:CRangeValidator 的别名,确保了特性出现在一个预订的值列表里.
Copy after login

unique: Uniqueness

The code is as follows:

['username', 'unique'] #说明:CUniqueValidator 的别名,确保了特性在数据表字段中是唯一的.
Copy after login

integer : Integer

['age', 'integer'];
Copy after login

number : Number

['salary', 'number'];
Copy after login

double : Double precisionFloating point

['salary', 'double'];
Copy after login

date : Date

[['from', 'to'], 'date'];
Copy after login

string : String

['username', 'string', 'length' => [4, 24]];
Copy after login

boolean: Whether it is a Boolean value

The code is as follows:

['字段名', 'boolean', 'trueValue' => true, 'falseValue' => false, 'strict' => true]; #说明:CBooleanValidator 的别名
Copy after login

image: Whether it is a valid image file

The code is as follows:

['primaryImage','image', 'extensions' => 'png, jpg,jpeg','minWidth' => 100,'maxWidth' => 1000,'minHeight' => 100,'maxHeight' => 1000,]
Copy after login

The above is the detailed content of Detailed introduction of Yii2.0 rules verification rule set. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template