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

Detailed introduction of Yii2.0 rules verification rule set

黄舟
黄舟Original
2017-03-22 09:13:571576browse

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 的别名, 确保了特性不为空.

email : Email verification

The code is as follows:

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

match: regular verification

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

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

url: URL

The code is as follows:

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

captcha: Verification code

The code is as follows:

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

safe : Safe

['description', 'safe'];

compare : Compare the

code as follows:

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

default : Default value

The code is as follows:

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

exist: Existence

The code is as follows:

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

file: File

The code is as follows:

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

filter: filter

The code is as follows:

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

in: Range

The code is as follows:

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

unique: Uniqueness

The code is as follows:

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

integer : Integer

['age', 'integer'];

number : Number

['salary', 'number'];

double : Double precisionFloating point

['salary', 'double'];

date : Date

[['from', 'to'], 'date'];

string : String

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

boolean: Whether it is a Boolean value

The code is as follows:

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

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,]

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!

Statement:
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