Yii2 rules經常使用規則

required : 必須值驗證屬性web

[['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #說明:CRequiredValidator 的別名, 確保了特性不爲空. 正則表達式

email : 郵箱驗證安全

['email', 'email']; #說明:CEmailValidator的別名,確保了特性的值是一個有效的電郵地址. ui

match : 正則驗證url

[['字段名'],match,'pattern'=>'正則表達式','message'=>'提示信息'];
[['字段名'],match,'not'=>ture,'pattern'=>'正則表達式','message'=>'提示信息']; /正則取反/ #說明:CRegularExpressionValidator 的別名, 確保了特性匹配一個正則表達式. code

url : 網址圖片

['website', 'url', 'defaultScheme' => 'http']; #說明:CUrlValidator 的別名, 確保了特性是一個有效的路徑. ip

captcha : 驗證碼字符串

['verificationCode', 'captcha']; #說明:CCaptchaValidator 的別名,確保了特性的值等於 CAPTCHA 顯示出來的驗證碼. string

safe : 安全

['description', 'safe'];

compare : 比較

['age', 'compare', 'compareValue' => 30, 'operator' => '>=']; #說明:compareValue(比較常量值) - operator(比較操做符) #說明:CCompareValidator 的別名,確保了特性的值等於另外一個特性或常量.

default : 默認值

['age', 'default', 'value' => null]; #說明:CDefaultValueValidator 的別名, 爲特性指派了一個默認值.

exist : 存在

['username', 'exist']; #說明:CExistValidator 的別名,確保屬性值存在於指定的數據表字段中.

file : 文件

['primaryImage', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 102410241024]; #說明:CFileValidator 的別名, 確保了特性包含了一個上傳文件的名稱.

filter : 過濾

[['username', 'email'], 'filter', 'filter' => 'trim', 'skipOnArray' => true]; #說明:CFilterValidator 的別名, 使用一個filter轉換屬性.

in : 範圍

['level', 'in', 'range' => [1, 2, 3]]; #說明:CRangeValidator 的別名,確保了特性出如今一個預訂的值列表裏.

unique : 惟一性

['username', 'unique'] #說明:CUniqueValidator 的別名,確保了特性在數據表字段中是惟一的.

integer : 整數

['age', 'integer'];

number : 數字

['salary', 'number'];

double : 雙精度浮點型

['salary', 'double'];

date : 日期

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

string : 字符串

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

boolean : 是否爲一個布爾值

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

image :是否爲有效的圖片文件

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

相關文章
相關標籤/搜索