YII2 驗證規則詳解

驗證規則

string

length

['phone', 'string', 'length'=>11, 'notEqual' => '手機號長度不正確'],

 [['password', 'name', 'code'], 'string', 'length' => [3,5] 'tooShort'=> '{attribute} 不能小於3個字符', 'tooLong' => '{$attribute} 不能大於5個字符'],

min

 ['username', 'string', 'min'=>3, 'tooShort' => '用戶名最少三個字符'],

max

 ['username', 'string', 'max'=>5, 'tooLong' => '用戶名最多五個個字符'],

boolean

trueValue, falseValue, strict

 ['is_vip', 'boolean ', 'trueValue'=>'1'//true值, 'falseValue' => '0'//false值,'strict'=>true//開啓嚴格驗證模式即爲===比較,默認爲false],

compare

message 佔位符

  • {attribute}: 正在驗證字段的label名稱
  • {value}: 正在被驗證字段的值
  • {compareValue}: 被比較的label名稱或值
  • {compareAttribute}: 被比較的字段
  • {compareValueOrAttribute}: 被比較的label名稱或值

operator :操作符

  • ==: 等於
  • ===: 全等於
  • !=: 不等於
  • !==: 不全等
  • >: 大於
  • >=: 大於等於
  • <: 小於
  • <=: 小於等於
['age', 'compare', 'compareAttribute' => 'default_age', 'operator'=>'>' ,'message' => '{attribute} 必須大於{compareAttribute}'],
['default_age', 'safe'],
發佈了34 篇原創文章 · 獲贊 11 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章