Yii驗證規則總結。

一、CRequiredValidator – 必須值驗證屬性正則表達式

requiredValue-mixed-所需的值
函數

strict-boolean-是否比較嚴格實例:ui

array(‘username’, ‘required’), 不能爲空 this

array(‘username’, ‘required’, ‘requiredValue’=>’lh’,'message’=> ‘usernmaemust be lh’), 這個值必須爲lh,若是填其餘值仍是會驗證不過編碼

array(‘username’, ‘required’, ‘requiredValue’=>’lh’, ‘strict’=>true), 嚴格驗證 還能夠在後面加 ‘message’=>」,’on’=>這些url

 

 

二、CFilterValidator 過濾驗證屬性orm

filter– 方法名 (調用用戶本身定義的函數) 實例: 字符串

array(‘username’, ‘test’),string

function test() { $username =$this->username; if($username != ‘lh’){$this->addError(‘username’, ‘username must be lh’); } } 使用這個方法若是你還在array裏面寫message=>」,給出的提示信息仍是你的test裏面的。也就是以test裏面的錯誤信息爲準it

 

 

三、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 23 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/yyyyhh: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 valuemust be 1 or -1′),

 

 

1七、CDateValidator – 日期驗證屬性:

allowEmpty – 是否爲空

format – 日期值應遵循的格式模式

timestampAttribute – 接收解析結果的屬性名稱 實例:

array(‘username’, ‘date’,‘format’=>’MM-dd-yyyy’,'message’=>’must be MM-dd-yyyy’),

array(‘username’, ‘date’,‘format’=>’MM/dd/yyyy’,'message’=>’must be MM/dd/yyyy’),

相關文章
相關標籤/搜索