1. 程式人生 > >yii rule 常用屬性方法

yii rule 常用屬性方法

array(

 array(‘username’, ‘required’),  array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12),  array(‘password’, ‘compare’, ‘compareAttribute’=>’password2′, ‘on’=>’register’),  array(‘password’, ‘authenticate’, ‘on’=>’login’),

  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驗證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

1、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’=>這些

2、CFilterValidator 過濾驗證屬性

filter – 方法名 (呼叫使用者自己定義的函式)

例項:

array(‘username’, ‘test’) function test() { $username = $this->username; if($username != ‘lh’){ $this->addError(‘username’, ‘username must be lh’); } }

使用這個方法如果你還在array裡面寫message=>”,給出的提示資訊還是你的test裡面的。也就是以test裡面的錯誤資訊為準

3、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’=>’必須不是字母’),

4、CEmailValidator –郵箱驗證屬性:

allowEmpty – 是否為空

allowName – 是否允許在電子郵件地址的名稱

checkMx – 是否檢查電子郵件地址的MX記錄

checkPort – 是否要檢查埠25的電子郵件地址

fullPattern – 正則表示式,用來驗證電子郵件地址與名稱的一部分

pattern – 正則表示式,

用來驗證的屬性值例項: array(‘username’, ‘email’, ‘message’=>’必須為電子郵箱’, ‘pattern’=>’/[a-z]/i’),

5、CUrlValidator – url驗證屬性:

allowEmpty – 是否為空

defaultScheme – 預設的URI方案

pattern – 正則表示式

validSchemes – 清單應視為有效的URI計劃。

例項:

array(‘username’, ‘url’, ‘message’=>’must url’),

array(‘username’, ‘url’, ‘defaultScheme’=>’http://www.baidu.com’),

6、CUniqueValidator – 唯一性驗證屬性:

allowEmpty – 是否為空

attributeName – 屬性名稱

caseSensitive – 區分大小寫

className – 類名

criteria – 額外的查詢條件

例項:

array(‘username’, ‘unique’, ‘message’=>’該記錄存在’),

array(‘username’, ‘unique’, ‘caseSensitive’=>false, ‘message’=>’該記錄存在’),

7、CCompareValidator – 比較驗證屬性:

allowEmpty – 是否為空

compareAttribute – 需要比較的屬性

compareValue -比較的值

operator – 比較運算子

strict – 嚴格驗證(值和型別都要相等)

例項: // 與某個值比較 array(‘username’, ‘compare’, ‘compareValue’=>’10′, ‘operator’=>’>’, ‘message’=>’必須大於10′),

// 與某個提交的屬性比較 array(‘username’, ‘compare’, ‘compareAttribute’=>’password’, ‘operator’=>’>’, ‘message’=>’必須大於password’),

8、CStringValidator – 字串驗證屬性:

allowEmpty – 是否為空

encoding – 編碼

is – 確切的長度

max – 最大長度

min – 最小長度

tooLong – 定義值太大的錯誤

tooShort – 定義最小長度的錯誤

例項: array(‘username’, ‘length’, ‘max’=>10, ‘min’=>5, ‘tooLong’=>’太長了’, ‘tooShort’=>’太短了’),

array(‘username’, ‘length’, ‘is’=>5, ‘message’=>’長度必須為5′),

9、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′),

10、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’),

11、CCaptchaValidator – 驗證碼驗證屬性:

allowEmpty – 是否為空

caseSensitive – 區分大小寫

12、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’),

13、CFileValidator – 檔案驗證屬性:

allowEmpty – 是否為空

maxFiles – 最大檔案數

maxSize – 檔案的最大值

minSize – 最小值

tooLarge – 太大時的錯誤資訊

tooMany – 太多時的錯誤資訊

tooSmall – 太小時的錯誤資訊

types – 允許的副檔名

wrongType – 副檔名錯誤時給出的錯誤資訊

14、CDefaultValueValidator – 預設值屬性:

setOnEmpty – 設定為空

value – 預設值

例項: array(‘username’, ‘default’, ‘setOnEmpty’=>true, ‘value’=>’lh’),

15、CExistValidator – 是否存在屬性:

allowEmpty = 是否為空

attributeName – 屬性名稱

className – 類名

criteria – 標準

16、CBooleanValidator – 布林型別驗證屬性:

allowEmpty – 是否為空

falseValue – 錯誤狀態的值

strict – 嚴格驗證

trueValue – 真實狀態的值

例項: array(‘username’, ‘boolean’, ‘trueValue’=>1, ‘falseValue’=>-1, ‘message’=>’the value must be 1 or -1′),

17、CDateValidator – 日期驗證屬性:

allowEmpty – 是否為空

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

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

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

--------------------- 本文來自 alashan007 的CSDN 部落格 ,全文地址請點選:https://blog.csdn.net/alashan007/article/details/78214210?utm_source=copy