去除首尾空白字符
['email', 'trim'] 或 ['email', 'filter', 'filter' => 'trim']
字段必填
['email', 'required']
賦予默認值
['age', 'default', 'value' => 18]
字符串長度
['email', 'string', 'min' => 3, 'max' => 20] 或 ['email', 'string', 'length' => [3, 20]]
格式類型驗證
// 整數格式 ['age', 'integer'] // 浮點數格式 ['salary', 'double'] // 數字格式 ['temperature', 'number'] // 布爾格式 ['isAdmin', 'boolean'] // email格式 ['email', 'email'] // 日期格式 ['birthday', 'date'] // URL格式 ['website', 'url', 'defaultScheme' => 'http']
驗證碼
['verificationCode', 'captcha']
值在數據表中是唯一的
['email', 'unique', 'targetClass' => 'commonmodelsUsers']
值在數據表中已存在
['email', 'exist', 'targetClass' => 'commonmodelsUser', 'filter' => ['status' => User::STATUS_ACTIVE], 'message' => 'There is no user with such email.'],
檢查輸入的兩個值是否一致
['passwordRepeat', 'required'] // 必須要加上這一句 ['passwordRepeat', 'compare', 'compareAttribute' => 'password', 'operator' => '===']
數值範圍檢查
['age', 'compare', 'compareValue' => 30, 'operator' => '>=']
['level', 'in', 'range' => [1, 2, 3]]
使用自定義函數過濾
['email', 'filter', 'filter' => function($value) { // 在此處標準化輸入的email return strtolower($value); }]
文件上傳
['textFile', 'file', 'extensions' => ['txt', 'rtf', 'doc'], 'maxSize' => 1024 * 1024 * 1024]
圖片上傳
['avatar', 'image', 'extensions' => ['png', 'jpg'], 'minWidth' => 100, 'maxWidth' => 1000, 'minHeight' => 100, 'maxHeight' => 1000, ]
使用正則表達式
['username', 'match', 'pattern' => '/^[a-z]w*$/i']