yii2.0 rules驗證規則大全

來源:互聯網
上載者:User
required : 必須值驗證屬性

[['欄位名'],required,'requiredValue'=>'必填值','message'=>'提示資訊']; #說明:CRequiredValidator 的別名, 確保了特性不為空白.

email : 郵箱驗證

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

match : 正則驗證

[['欄位名'],match,'pattern'=>'Regex','message'=>'提示資訊'];      [['欄位名'],match,'not'=>ture,'pattern'=>'Regex','message'=>'提示資訊']; /*正則取反*/ #說明:CRegularExpressionValidator 的別名, 確保了特性匹配一個Regex.

url : 網址

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

captcha : 驗證碼

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

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' => 1024*1024*1024]; #說明: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,]

有漏的大家可以留言補充。互相學習

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.