CBooleanValidator
鍖 | system.validators |
---|---|
缁ф壙 | class CBooleanValidator » CValidator » CComponent |
鐗堟湰 | $Id: CBooleanValidator.php 3515 2011-12-28 12:29:24Z mdomba $ |
婧愮爜 | framework/validators/CBooleanValidator.php |
CBooleanValidator 楠岃瘉trueValue鎴栬falseValue灞炴у笺
褰撲娇鐢message灞炴у煎幓瀹氫箟涓涓嚜瀹氫箟閿欒淇℃伅锛岃繖涓俊鎭 鍙兘浼氬寘鍚竴浜涢檮鍔犵殑浼氳瀹為檯鐨勫唴瀹逛唬鏇跨殑鍗犱綅绗︺傛瘮濡傚浜"{attribute}"鍗犱綅绗︼紝 鍙互琚墍鏈夌殑楠岃瘉鍣ㄨ瘑鍒(鍙傝CValidator)锛 CBooleanValidator 鍏佽濡備笅鐨勫崰浣嶇鎸囧畾锛
褰撲娇鐢message灞炴у煎幓瀹氫箟涓涓嚜瀹氫箟閿欒淇℃伅锛岃繖涓俊鎭 鍙兘浼氬寘鍚竴浜涢檮鍔犵殑浼氳瀹為檯鐨勫唴瀹逛唬鏇跨殑鍗犱綅绗︺傛瘮濡傚浜"{attribute}"鍗犱綅绗︼紝 鍙互琚墍鏈夌殑楠岃瘉鍣ㄨ瘑鍒(鍙傝CValidator)锛 CBooleanValidator 鍏佽濡備笅鐨勫崰浣嶇鎸囧畾锛
- {true}: 琚敤浜庤〃绀虹湡鐨 trueValue鍊兼浛鎹€
- {false}: 琚敤浜庤〃绀哄亣鐨falseValue鍊兼浛鎹€
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
allowEmpty | boolean | 鏄惁鍏佽灞炴у糿ull鎴栬卐mpty锛岄粯璁ゆ槸鐪熷硷紝 琛ㄧず濡傛灉灞炴у兼槸empty锛屼粬琛ㄧず鏄棤鏁堝笺 | CBooleanValidator |
attributes | array | 闇瑕佽楠岃瘉鐨勫睘鎬х殑鍒楄〃銆 | CValidator |
builtInValidators | array | 鍐呯疆楠岃瘉鍣ㄥ垪琛 (name=>class) | CValidator |
enableClientValidation | boolean | 鏄惁鎵ц瀹㈡埛绔獙璇併傞粯璁ゅ间负true銆 鍙傝CActiveForm::enableClientValidation浠ヤ簡瑙f洿澶氬叧浜庡鎴风楠岃瘉鐨勭粏鑺傘 | CValidator |
falseValue | mixed | 鐢ㄤ簬浠h〃鐪熷肩姸鎬併傞粯璁ゆ槸 '0'銆 | CBooleanValidator |
message | string | 鐢ㄦ埛鑷畾涔夌殑閿欒鎻愮ず淇℃伅銆備笉鍚岀殑楠岃瘉鍣ㄥ彲浠ュ湪璇ヤ俊鎭腑 瀹氫箟鍚勭鍗犱綅绗︼紙灏嗚瀹為檯鍊兼浛鎹級銆傚崰浣嶇鈥渰attribute}鈥濆彲浠ヨ鎵鏈 楠岃瘉鍣ㄨ瘑鍒紝瀹冧細琚娇鐢ㄥ睘鎬х殑鏍囩鏉ユ浛鎹€ | CValidator |
on | array | 楠岃瘉鍣ㄥ皢琚簲鐢ㄥ埌鐨勬儏鏅ā寮忕殑鍒楄〃銆 鏁扮粍鐨勯敭-鍊奸兘鏄儏鏅ā寮忕殑鍚嶇О銆 | CValidator |
safe | boolean | 杩涜鏁村潡璧嬪兼槸鏄惁鑰冭檻姝ら獙璇佸櫒涓垪鍑虹殑灞炴х殑瀹夊叏鎬с 榛樿鍊间负true銆 | CValidator |
skipOnError | boolean | 濡傛灉褰撳墠灞炴у凡缁忓瓨鍦ㄩ獙璇侀敊璇紝杩欎釜楠岃瘉瑙勫垯 鏄惁璺宠繃銆傞粯璁ゅ兼槸false銆 | CValidator |
strict | boolean | 姣旇緝 trueValue 鍜 falseValue 鏃舵槸鍚︽槸涓ユ牸鍖归厤鐨勩 褰撹繖涓殑鍊兼槸鐪熷肩殑鏃跺欙紝杩欎釜鐨勫睘鎬у煎拰绫诲瀷蹇呴』鍜trueValue鎴栬falseValue鍖归厤銆 榛樿鍊兼槸鍋囷紝琛ㄧず鍙槸杩欎釜鍊奸渶瑕佸尮閰嶃 | CBooleanValidator |
trueValue | mixed | 鐢ㄤ簬浠h〃鐪熷肩姸鎬併 榛樿鏄'1'銆 | CBooleanValidator |
鍏叡鏂规硶
鍙椾繚鎶ゆ柟娉
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
addError() | 娣诲姞鍏充簬鎸囧畾灞炴х殑涓涓敊璇彁绀轰俊鎭埌娲诲姩璁板綍涓 | CValidator |
isEmpty() | 妫娴嬬粰瀹氬兼槸鍚︿负绌恒 | CValidator |
validateAttribute() | 瀵圭洰鏍囩殑灞炴ц繘琛岄獙璇併 | CBooleanValidator |
灞炴ц缁
allowEmpty
灞炴
public boolean $allowEmpty;
鏄惁鍏佽灞炴у糿ull鎴栬卐mpty锛岄粯璁ゆ槸鐪熷硷紝 琛ㄧず濡傛灉灞炴у兼槸empty锛屼粬琛ㄧず鏄棤鏁堝笺
falseValue
灞炴
public mixed $falseValue;
鐢ㄤ簬浠h〃鐪熷肩姸鎬併傞粯璁ゆ槸 '0'銆
strict
灞炴
public boolean $strict;
姣旇緝 trueValue 鍜 falseValue 鏃舵槸鍚︽槸涓ユ牸鍖归厤鐨勩 褰撹繖涓殑鍊兼槸鐪熷肩殑鏃跺欙紝杩欎釜鐨勫睘鎬у煎拰绫诲瀷蹇呴』鍜trueValue鎴栬falseValue鍖归厤銆 榛樿鍊兼槸鍋囷紝琛ㄧず鍙槸杩欎釜鍊奸渶瑕佸尮閰嶃
trueValue
灞炴
public mixed $trueValue;
鐢ㄤ簬浠h〃鐪熷肩姸鎬併 榛樿鏄'1'銆
鏂规硶璇︾粏
clientValidateAttribute()
鏂规硶
锛堝彲鐢ㄨ嚜 v1.1.7锛
public string clientValidateAttribute(CModel $object, string $attribute)
| ||
$object | CModel | 琚獙璇佺殑瀵硅薄 |
$attribute | string | 琚獙璇佺殑灞炴х殑鍚嶅瓧銆 |
{return} | string | 瀹㈡埛绔獙璇佺殑鑴氭湰銆 |
婧愮爜锛 framework/validators/CBooleanValidator.php#79 (鏄剧ず)
public function clientValidateAttribute($object,$attribute)
{
$message=$this->message!==null ? $this->message : Yii::t('yii','{attribute} must be either {true} or {false}.');
$message=strtr($message, array(
'{attribute}'=>$object->getAttributeLabel($attribute),
'{true}'=>$this->trueValue,
'{false}'=>$this->falseValue,
));
return "
if(".($this->allowEmpty ? "$.trim(value)!='' && " : '')."value!=".CJSON::encode($this->trueValue)." && value!=".CJSON::encode($this->falseValue).") {
messages.push(".CJSON::encode($message).");
}
";
}
杩斿洖鐢ㄤ簬瀹㈡埛绔獙璇佺殑JavaScript銆
validateAttribute()
鏂规硶
protected void validateAttribute(CModel $object, string $attribute)
| ||
$object | CModel | 鐢ㄤ簬楠岃瘉鐨勫璞 |
$attribute | string | 鐢ㄤ簬楠岃瘉鐨勫睘鎬 |
婧愮爜锛 framework/validators/CBooleanValidator.php#55 (鏄剧ず)
protected function validateAttribute($object,$attribute)
{
$value=$object->$attribute;
if($this->allowEmpty && $this->isEmpty($value))
return;
if(!$this->strict && $value!=$this->trueValue && $value!=$this->falseValue
|| $this->strict && $value!==$this->trueValue && $value!==$this->falseValue)
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be either {true} or {false}.');
$this->addError($object,$attribute,$message,array(
'{true}'=>$this->trueValue,
'{false}'=>$this->falseValue,
));
}
}
瀵圭洰鏍囩殑灞炴ц繘琛岄獙璇併 濡傛灉杩欎釜鍑虹幇浠讳綍閿欒锛岃繖涓敊璇皢琚姞鍒拌繖涓璞′腑鍘汇