CRangeValidator
| 鍖 | system.validators |
|---|---|
| 缁ф壙 | class CRangeValidator » CValidator » CComponent |
| 婧愯嚜 | 1.0 |
| 鐗堟湰 | $Id: CRangeValidator.php 3120 2011-03-25 01:50:48Z qiang.xue $ |
| 婧愮爜 | framework/validators/CRangeValidator.php |
鍏叡灞炴
| 灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
|---|---|---|---|
| allowEmpty | boolean | 鏄惁姝ゅ睘鎬у煎彲浠ヤ负null鎴杄mpty銆傞粯璁ゅ兼槸true锛 琛ㄧず濡傛灉姝ゅ睘鎬у间负绌烘椂灏嗛氳繃楠岃瘉銆 | CRangeValidator |
| attributes | array | 闇瑕佽楠岃瘉鐨勫睘鎬х殑鍒楄〃銆 | CValidator |
| builtInValidators | array | 鍐呯疆楠岃瘉鍣ㄥ垪琛 (name=>class) | CValidator |
| enableClientValidation | boolean | 鏄惁鎵ц瀹㈡埛绔獙璇併傞粯璁ゅ间负true銆 鍙傝CActiveForm::enableClientValidation浠ヤ簡瑙f洿澶氬叧浜庡鎴风楠岃瘉鐨勭粏鑺傘 | CValidator |
| message | string | 鐢ㄦ埛鑷畾涔夌殑閿欒鎻愮ず淇℃伅銆備笉鍚岀殑楠岃瘉鍣ㄥ彲浠ュ湪璇ヤ俊鎭腑 瀹氫箟鍚勭鍗犱綅绗︼紙灏嗚瀹為檯鍊兼浛鎹級銆傚崰浣嶇鈥渰attribute}鈥濆彲浠ヨ鎵鏈 楠岃瘉鍣ㄨ瘑鍒紝瀹冧細琚娇鐢ㄥ睘鎬х殑鏍囩鏉ユ浛鎹€ | CValidator |
| not | boolean | 鏄惁鍙嶈浆楠岃瘉閫昏緫銆傞粯璁ゅ兼槸false銆傚鏋滆缃负true锛 姝ゅ睘鎬у涓嶅湪鍒楄〃锛堥氳繃range瀹氫箟锛変腑鏃堕獙璇侀氳繃銆 | CRangeValidator |
| on | array | 楠岃瘉鍣ㄥ皢琚簲鐢ㄥ埌鐨勬儏鏅ā寮忕殑鍒楄〃銆 鏁扮粍鐨勯敭-鍊奸兘鏄儏鏅ā寮忕殑鍚嶇О銆 | CValidator |
| range | array | 姝ゅ睘鎬у肩殑鏈夋晥鍊煎垪琛 | CRangeValidator |
| safe | boolean | 杩涜鏁村潡璧嬪兼槸鏄惁鑰冭檻姝ら獙璇佸櫒涓垪鍑虹殑灞炴х殑瀹夊叏鎬с 榛樿鍊间负true銆 | CValidator |
| skipOnError | boolean | 濡傛灉褰撳墠灞炴у凡缁忓瓨鍦ㄩ獙璇侀敊璇紝杩欎釜楠岃瘉瑙勫垯 鏄惁璺宠繃銆傞粯璁ゅ兼槸false銆 | CValidator |
| strict | boolean | 鏄惁鎵ц涓ユ牸姣旇緝锛堢被鍨嬩笌鍊奸兘蹇呴』涓鑷达級銆傞粯璁ゅ兼槸false銆 | CRangeValidator |
鍏叡鏂规硶
鍙椾繚鎶ゆ柟娉
| 鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
|---|---|---|
| addError() | 娣诲姞鍏充簬鎸囧畾灞炴х殑涓涓敊璇彁绀轰俊鎭埌娲诲姩璁板綍涓 | CValidator |
| isEmpty() | 妫娴嬬粰瀹氬兼槸鍚︿负绌恒 | CValidator |
| validateAttribute() | 楠岃瘉浼犲叆瀵硅薄鐨勫睘鎬с | CRangeValidator |
灞炴ц缁
allowEmpty
灞炴
public boolean $allowEmpty;
鏄惁姝ゅ睘鎬у煎彲浠ヤ负null鎴杄mpty銆傞粯璁ゅ兼槸true锛 琛ㄧず濡傛灉姝ゅ睘鎬у间负绌烘椂灏嗛氳繃楠岃瘉銆
not
灞炴
锛堝彲鐢ㄨ嚜 v1.1.5锛
public boolean $not;
鏄惁鍙嶈浆楠岃瘉閫昏緫銆傞粯璁ゅ兼槸false銆傚鏋滆缃负true锛 姝ゅ睘鎬у涓嶅湪鍒楄〃锛堥氳繃range瀹氫箟锛変腑鏃堕獙璇侀氳繃銆
range
灞炴
public array $range;
姝ゅ睘鎬у肩殑鏈夋晥鍊煎垪琛
strict
灞炴
public boolean $strict;
鏄惁鎵ц涓ユ牸姣旇緝锛堢被鍨嬩笌鍊奸兘蹇呴』涓鑷达級銆傞粯璁ゅ兼槸false銆
鏂规硶璇︾粏
clientValidateAttribute()
鏂规硶
锛堝彲鐢ㄨ嚜 v1.1.7锛
|
public string clientValidateAttribute(CModel $object, string $attribute)
| ||
| $object | CModel | 闇瑕侀獙璇佺殑瀵硅薄 |
| $attribute | string | 闇瑕侀獙璇佺殑灞炴 |
| {return} | string | 瀹㈡埛绔獙璇佽剼鏈 |
婧愮爜锛 framework/validators/CRangeValidator.php#75 (鏄剧ず)
public function clientValidateAttribute($object,$attribute)
{
if(!is_array($this->range))
throw new CException(Yii::t('yii','The "range" property must be specified with a list of values.'));
if(($message=$this->message)===null)
$message=$this->not ? Yii::t('yii','{attribute} is in the list.') : Yii::t('yii','{attribute} is not in the list.');
$message=strtr($message,array(
'{attribute}'=>$object->getAttributeLabel($attribute),
));
$range=array();
foreach($this->range as $value)
$range[]=(string)$value;
$range=CJSON::encode($range);
return "
if(".($this->allowEmpty ? "$.trim(value)!='' && " : '').($this->not ? "$.inArray(value, $range)>=0" : "$.inArray(value, $range)<0").") {
messages.push(".CJSON::encode($message).");
}
";
}
杩斿洖鎵ц瀹㈡埛绔獙璇佹墍闇鐨凧avaScript銆
validateAttribute()
鏂规硶
|
protected void validateAttribute(CModel $object, string $attribute)
| ||
| $object | CModel | 闇瑕侀獙璇佺殑瀵硅薄 |
| $attribute | string | 闇瑕侀獙璇佺殑灞炴 |
婧愮爜锛 framework/validators/CRangeValidator.php#48 (鏄剧ず)
protected function validateAttribute($object,$attribute)
{
$value=$object->$attribute;
if($this->allowEmpty && $this->isEmpty($value))
return;
if(!is_array($this->range))
throw new CException(Yii::t('yii','The "range" property must be specified with a list of values.'));
if(!$this->not && !in_array($value,$this->range,$this->strict))
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is not in the list.');
$this->addError($object,$attribute,$message);
}
else if($this->not && in_array($value,$this->range,$this->strict))
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} is in the list.');
$this->addError($object,$attribute,$message);
}
}
楠岃瘉浼犲叆瀵硅薄鐨勫睘鎬с 濡傛灉瀛樺湪浠讳綍楠岃瘉閿欒锛岄敊璇彁绀轰俊鎭皢琚坊鍔犲埌姝ゅ璞′腑銆