array( 正則表達式
array(‘username’, ‘required’),
array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12),
array(‘password’, ‘compare’, ‘compareAttribute’=>’password2′, ‘on’=>’register’),
array(‘password’, ‘authenticate’, ‘on’=>’login’),api
array(‘Price’,'numerical’, ‘integerOnly’=>true),
);
public function rules()
{
return array(
array(‘title, content, status’, ‘required’),
array(‘title’, ‘length’, ‘max’=>128),
array(‘status’, ‘in’, ‘range’=>array(1,2,3)),
array(‘tags’, ‘match’, ‘pattern’=>’/^[\w\s,]+$/’,
’message’=>’Tags can only contain word characters.’),
array(‘tags’, ‘normalizeTags’),
array(‘title, status’, ‘safe’, ‘on’=>’search’),
);
}預約義完整列表: yii
boolean
函數
: ui
CBooleanValidator this
的別名, 確保屬性的值是CBooleanValidator::trueValue 編碼
或CBooleanValidator::falseValue url
.spa
captcha
code
:
的別名,確保了特性的值等於
顯示出來的驗證碼.
compare
:
的別名, 確保了特性的值等於另外一個特性或常量.
email
:
的別名,確保了特性的值是一個有效的電郵地址.
default
:
的別名, 爲特性指派了一個默認值.
exist
:
的別名, 確保屬性值存在於指定的數據表字段中.
file
:
的別名, 確保了特性包含了一個上傳文件的名稱.
filter
:
的別名, 使用一個filter轉換屬性.
in
:
的別名, 確保了特性出如今一個預訂的值列表裏.
length
:
的別名, 確保了特性的長度在指定的範圍內.
match
:
的別名, 確保了特性匹配一個正則表達式.
numerical
:
的別名, 確保了特性是一個有效的數字.
required
:
的別名, 確保了特性不爲空.
type
:
的別名, 確保了特性爲指定的數據類型.
unique
:
的別名, 確保了特性在數據表字段中是惟一的.
url
:
的別名, 確保了特性是一個有效的路徑
yii驗證rulesit 分類: Yii yii的rules驗證 cValidator主要屬性 attributes ,builtInValidators,enableClientValidation,message,on,safe,skipOnError
常常用到的屬性有 attributes,builtInvalidators,message,on這四個
下面是對應的驗證類
required: CRequiredValidator
filter: CFilterValidator
match: CRegularExpressionValidator
email: CEmailValidator
url: CUrlValidator
unique: CUniqueValidator
compare: CCompareValidator
length: CStringValidator
in: CRangeValidator
numerical: CNumberValidator
captcha: CCaptchaValidator
type: CTypeValidator
file: CFileValidator
default: CDefaultValueValidator
exist: CExistValidator
boolean: CBooleanValidator
date: CDateValidator
safe: CSafeValidator
unsafe: CUnsafeValidator
一、CRequiredValidator – 必須值驗證屬性
requiredValue-mixed-所需的值
strict-boolean-是否比較嚴格
實例: array(‘username’, ‘required’), 不能爲空
array(‘username’, ‘required’, ‘requiredValue’=>’lh’,’message’=> ‘usernmae must be lh’), 這個值必須爲lh,若是填其餘值仍是會驗證不過
array(‘username’, ‘required’, ‘requiredValue’=>’lh’, ‘strict’=>true), 嚴格驗證 還能夠在後面加 ‘message’=>」,’on’=>這些
二、CFilterValidator 過濾驗證屬性
filter – 方法名 (調用用戶本身定義的函數)
實例:
array(‘username’, ‘test’) function test() { $username = $this->username; if($username != ‘lh’){ $this->addError(‘username’, ‘username must be lh’); } }
使用這個方法若是你還在array裏面寫message=>」,給出的提示信息仍是你的test裏面的。也就是以test裏面的錯誤信息爲準
三、CRegularExpressionValidator -
正則驗證屬性allowEmpty – 是否爲空(默認true)
not-是否反轉的驗證邏輯(默認false) pattern – 正則表達式匹配實例:
// 匹配a-z array(‘username’, ‘match’, ‘allowEmpty’=>true, ‘pattern’=>’/[a-z]/i’,’message’=>’必須爲字母’),
// 匹配不是a-z array(‘username’, ‘match’, ‘allowEmpty’=>true, ‘not’=>true, ‘pattern’=>’/[a-z]/i’,’message’=>’必須不是字母’),
四、CEmailValidator –郵箱驗證屬性:
allowEmpty – 是否爲空
allowName – 是否容許在電子郵件地址的名稱
checkMx – 是否檢查電子郵件地址的MX記錄
checkPort – 是否要檢查端口25的電子郵件地址
fullPattern – 正則表達式,用來驗證電子郵件地址與名稱的一部分
pattern – 正則表達式,
用來驗證的屬性值實例: array(‘username’, ‘email’, ‘message’=>’必須爲電子郵箱’, ‘pattern’=>’/[a-z]/i’),
五、CUrlValidator – url驗證屬性:
allowEmpty – 是否爲空
defaultScheme – 默認的URI方案
pattern – 正則表達式
validSchemes – 清單應視爲有效的URI計劃。
實例:
array(‘username’, ‘url’, ‘message’=>’must url’),
array(‘username’, ‘url’, ‘defaultScheme’=>’http://www.baidu.com’),
六、CUniqueValidator – 惟一性驗證屬性:
allowEmpty – 是否爲空
attributeName – 屬性名稱
caseSensitive – 區分大小寫
className – 類名
criteria – 額外的查詢條件
實例:
array(‘username’, ‘unique’, ‘message’=>’該記錄存在’),
array(‘username’, ‘unique’, ‘caseSensitive’=>false, ‘message’=>’該記錄存在’),
七、CCompareValidator – 比較驗證屬性:
allowEmpty – 是否爲空
compareAttribute – 須要比較的屬性
compareValue -比較的值
operator – 比較運算符
strict – 嚴格驗證(值和類型都要相等)
實例: // 與某個值比較 array(‘username’, ‘compare’, ‘compareValue’=>’10′, ‘operator’=>’>’, ‘message’=>’必須大於10′),
// 與某個提交的屬性比較 array(‘username’, ‘compare’, ‘compareAttribute’=>’password’, ‘operator’=>’>’, ‘message’=>’必須大於password’),
八、CStringValidator – 字符串驗證屬性:
allowEmpty – 是否爲空
encoding – 編碼
is – 確切的長度
max – 最大長度
min – 最小長度
tooLong – 定義值太大的錯誤
tooShort – 定義最小長度的錯誤
實例: array(‘username’, ‘length’, ‘max’=>10, ‘min’=>5, ‘tooLong’=>’太長了’, ‘tooShort’=>’過短了’),
array(‘username’, ‘length’, ‘is’=>5, ‘message’=>’長度必須爲5′),
九、CRangeValidator – 在某個範圍內屬性:
allowEmpty – 是否爲空
not – 是否反轉的驗證邏輯。
range – array範圍
strict – 嚴格驗證(類型和值都要同樣)
實例: array(‘username’, ‘in’, ‘range’=>array(1,2,3,4,5), ‘message’=>’must in 1 2 3 4 5′),
array(‘username’, ‘in’, ‘not’=>true, ‘range’=>array(1,2,3,4,5), ‘message’=>’must not in 1 2 3 4 5′),
十、CNumberValidator – 數字驗證屬性:
allowEmpty – 是否爲空
integerOnly – 整數
integerPattern – 正則表達式匹配整數
max – 最大值
min – 最小值
numberPattern – 匹配號碼
tooBig – 值太大時的錯誤提示
tooSmall – 值過小時的錯誤提示
實例: array(‘username’, ‘numerical’, ‘integerOnly’=>true, ‘message’=>’must be int’),
array(‘username’, ‘numerical’, ‘integerOnly’=>true, ‘message’=>’must be int’, ‘max’=>100, ‘min’=>10, ‘tooBig’=>’is too big’, ‘tooSmall’=>’is too small’),
十一、CCaptchaValidator – 驗證碼驗證屬性:
allowEmpty – 是否爲空
caseSensitive – 區分大小寫
十二、CTypeValidator – 類型驗證屬性:
allowEmpty – 是否爲空
dateFormat – 日期應遵循的格式模式(‘MM/dd/yyyy’)
datetimeFormat – 日期時間應遵循的格式模式(‘MM/dd/yyyy hh:mm’)
timeFormat – 時間應遵循的格式模式(‘hh:mm’)
type – 類型 ‘string’, ‘integer’, ‘float’, ‘array’, ‘date’, ‘time’ and ‘datetime’
實例: array(‘username’, ‘type’, ‘dateFormat’=>’MM/dd/yyyy’, ‘type’=>’date’),
1三、CFileValidator – 文件驗證屬性:
allowEmpty – 是否爲空
maxFiles – 最大文件數
maxSize – 文件的最大值
minSize – 最小值
tooLarge – 太大時的錯誤信息
tooMany – 太多時的錯誤信息
tooSmall – 過小時的錯誤信息
types – 容許的文件擴展名
wrongType – 擴展名錯誤時給出的錯誤信息
1四、CDefaultValueValidator – 默認值屬性:
setOnEmpty – 設置爲空
value – 默認值
實例: array(‘username’, ‘default’, ‘setOnEmpty’=>true, ‘value’=>’lh’),
1五、CExistValidator – 是否存在屬性:
allowEmpty = 是否爲空
attributeName – 屬性名稱
className – 類名
criteria – 標準
1六、CBooleanValidator – 布爾類型驗證屬性:
allowEmpty – 是否爲空
falseValue – 錯誤狀態的值
strict – 嚴格驗證
trueValue – 真實狀態的值
實例: array(‘username’, ‘boolean’, ‘trueValue’=>1, ‘falseValue’=>-1, ‘message’=>’the value must be 1 or -1′),
1七、CDateValidator – 日期驗證屬性:
allowEmpty – 是否爲空
format – 日期值應遵循的格式模式
timestampAttribute – 接收解析結果的屬性名稱
實例: array(‘username’, ‘date’, ‘format’=>’MM-dd-yyyy’,’message’=>’must be MM-dd-yyyy’),