天天看點

yii rules 驗證詳解

yii rules 驗證詳解

  1. public function rules()  
  2. {  
  3.     return array(  
  4.         array('project_id, type_id, status_id, owner_id, requester_id,', 'numerical', 'integerOnly'=>true),  
  5.         array('name', 'length', 'max'=>256),  
  6.         array('description', 'length', 'max'=>2000),  
  7.         array('create_time,create_user_id,update_user_id, update_time', 'safe'),  
  8.         array('id, name, description, project_id, type_id, status_id, owner_id', 'on'=>'search'),  
  9.     );  
  10. }  
  11. //required: 必填  
  12. array('title,content','required'),  
  13. //match: 正規表達式驗證  
  14. array('birthday', 'match', 'pattern'=>'%^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$%', 'allowEmpty'=>true, 'message'=>'生日必須是年-月-日格式'),  
  15. //email:郵箱格式驗證  
  16. array('user_mail', 'email'),   
  17. //url:URL格式驗證  
  18. array('user', 'url'),   
  19. //unique:唯一性驗證  
  20. array('username', 'unique','caseSensitive'=>false,'className'=>'user','message'=>'使用者名"{value}"已經被注冊,請更換'),  
  21. //caseSensitive 定義大小寫是否敏感  
  22. //compare:一緻性驗證  
  23. array('repassword', 'compare', 'compareAttribute'=>'password','message'=>'兩處輸入的密碼并不一緻'),  
  24. //length:長度驗證   
  25. //in: 驗證此屬性值在清單之中(通過range指定)。  
  26. //numerical: 驗證此屬性的值是一個數字  
  27. //captcha: 驗證屬性值和驗證碼中顯示的一緻  
  28. array('verifyCode','captcha'),  
  29. //type: 驗證屬性的類型是否為type所指定的類型.   
  30. //file: 驗證一個屬性是否接收到一個有效的上傳檔案  
  31. //default: 屬性指定預設值  
  32. //exist: 驗證屬性值在資料庫中是否存在  
  33. //boolean: 驗證布爾屬性值  
  34. //date: 檢驗此屬性是否描述了一個日期、時間或日期時間  
  35. //safe: 屬性标志為在批量指派時是安全的。  
  36. //unsafe: 标志為不安全,是以他們不能被批量指派。 

二、Yii CModel.rules()方法 、validate預定義完整清單

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

  );

}預定義完整清單: 

  • boolean

     : CBooleanValidator 的别名, 確定屬性的值是CBooleanValidator::trueValue 或CBooleanValidator::falseValue .
  • captcha

     : CCaptchaValidator 的别名,確定了特性的值等于 CAPTCHA 顯示出來的驗證碼.
  • compare

     : CCompareValidator 的别名, 確定了特性的值等于另一個特性或常量.
  • email

     : CEmailValidator 的别名,確定了特性的值是一個有效的電郵位址.
  • default

     : CDefaultValueValidator 的别名, 為特性指派了一個預設值.
  • exist

     : CExistValidator 的别名, 確定屬性值存在于指定的資料表字段中.
  • file

     : CFileValidator 的别名, 確定了特性包含了一個上傳檔案的名稱.
  • filter

     : CFilterValidator 的别名, 使用一個filter轉換屬性.
  • in

     : CRangeValidator 的别名, 確定了特性出現在一個預訂的值清單裡.
  • length

     : CStringValidator 的别名, 確定了特性的長度在指定的範圍内.
  • match

     : CRegularExpressionValidator 的别名, 確定了特性比對一個正規表達式.
  • numerical

     : CNumberValidator 的别名, 確定了特性是一個有效的數字.
  • required

     : CRequiredValidator 的别名, 確定了特性不為空.
  • type

     : CTypeValidator 的别名, 確定了特性為指定的資料類型.
  • unique

     : CUniqueValidator 的别名, 確定了特性在資料表字段中是唯一的.
  • url

     : CUrlValidator 的别名, 確定了特性是一個有效的路徑 

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’)