CCompareValidator
鍖 | system.validators |
---|---|
缁ф壙 | class CCompareValidator » CValidator » CComponent |
婧愯嚜 | 1.0 |
鐗堟湰 | $Id: CCompareValidator.php 3515 2011-12-28 12:29:24Z mdomba $ |
婧愮爜 | framework/validators/CCompareValidator.php |
CCompareValidator浣跨敤缁欏畾鐨勫睘鎬у间笌鍙︿竴涓艰繘琛屾瘮杈冿紝楠岃瘉浠栦滑鏄惁鐩哥瓑銆
鐢ㄤ簬姣旇緝鐨勫煎彲浠ユ槸鍙︿竴涓睘鎬х殑鍊 锛堥氳繃compareAttribute鎸囧畾锛夋垨涓涓父閲 锛堥氳繃compareValue鎸囧畾锛夈傚鏋滆繖涓ょ鎯 褰㈤兘鎸囧畾浜嗭紝閭d箞鍚庝竴涓紭鍏堛傚鏋滄病鏈夋寚瀹氫换浣曟儏 褰紝灏嗕娇鐢ㄥ彟涓涓睘鎬х殑鍊艰繘琛屾瘮杈冿紝璇ュ睘鎬х殑鍚嶅瓧鏄 鍦ㄩ渶姣旇緝鐨勫睘鎬х殑鍚嶅瓧鍚庡姞涓娾淿repeat鈥濈粍鎴愩
姣旇緝鐨勬ā寮忓彲浠ユ槸涓ユ牸姣旇緝锛strict锛夛紝涔熷彲浠ヤ笉鏄
CCompareValidator鐜板湪鏀寔澶氱姣旇緝鎿嶄綔绗︼紝 涔嬪墠锛屽畠鍙槸姣旇緝骞舵娴嬩袱涓兼槸鍚︾浉绛夈
褰撲娇鐢message灞炴у畾涔変簡涓涓嚜瀹氫箟閿欒娑堟伅锛屾娑堟伅 鍙互鍖呭惈闄勫姞鐨勫崰浣嶇锛岃繖浜涘崰浣嶇灏嗚瀹為檯鍐呭鏇挎崲銆傞櫎浜嗘墍鏈 楠岃瘉鍣ㄩ兘鍙互璇嗗埆鐨勨渰attribute}鈥濆崰浣嶇涔嬪(鍙傝CValidator)锛 CCompareValidator杩樺厑璁告寚瀹氫笅鍒楀崰浣嶇锛
鐢ㄤ簬姣旇緝鐨勫煎彲浠ユ槸鍙︿竴涓睘鎬х殑鍊 锛堥氳繃compareAttribute鎸囧畾锛夋垨涓涓父閲 锛堥氳繃compareValue鎸囧畾锛夈傚鏋滆繖涓ょ鎯 褰㈤兘鎸囧畾浜嗭紝閭d箞鍚庝竴涓紭鍏堛傚鏋滄病鏈夋寚瀹氫换浣曟儏 褰紝灏嗕娇鐢ㄥ彟涓涓睘鎬х殑鍊艰繘琛屾瘮杈冿紝璇ュ睘鎬х殑鍚嶅瓧鏄 鍦ㄩ渶姣旇緝鐨勫睘鎬х殑鍚嶅瓧鍚庡姞涓娾淿repeat鈥濈粍鎴愩
姣旇緝鐨勬ā寮忓彲浠ユ槸涓ユ牸姣旇緝锛strict锛夛紝涔熷彲浠ヤ笉鏄
CCompareValidator鐜板湪鏀寔澶氱姣旇緝鎿嶄綔绗︼紝 涔嬪墠锛屽畠鍙槸姣旇緝骞舵娴嬩袱涓兼槸鍚︾浉绛夈
褰撲娇鐢message灞炴у畾涔変簡涓涓嚜瀹氫箟閿欒娑堟伅锛屾娑堟伅 鍙互鍖呭惈闄勫姞鐨勫崰浣嶇锛岃繖浜涘崰浣嶇灏嗚瀹為檯鍐呭鏇挎崲銆傞櫎浜嗘墍鏈 楠岃瘉鍣ㄩ兘鍙互璇嗗埆鐨勨渰attribute}鈥濆崰浣嶇涔嬪(鍙傝CValidator)锛 CCompareValidator杩樺厑璁告寚瀹氫笅鍒楀崰浣嶇锛
- {compareValue}: 鐢ㄤ簬涓compareValue鐩告瘮杈冪殑甯搁噺鐨勫笺
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
allowEmpty | boolean | 姝ゅ睘鎬у兼槸鍚﹀彲浠ヤ负null鎴杄mpty銆傞粯璁ゅ间负false銆 濡傛灉璁剧疆涓簍rue锛岃〃绀哄綋瀹冧负绌烘椂姝ゅ睘鎬т篃浼氶氳繃楠岃瘉銆 | CCompareValidator |
attributes | array | 闇瑕佽楠岃瘉鐨勫睘鎬х殑鍒楄〃銆 | CValidator |
builtInValidators | array | 鍐呯疆楠岃瘉鍣ㄥ垪琛 (name=>class) | CValidator |
compareAttribute | string | 鐢ㄤ簬姣旇緝鐨勫睘鎬х殑鍚嶅瓧銆 | CCompareValidator |
compareValue | string | 鐢ㄤ簬姣旇緝鐨勫父閲忓 | CCompareValidator |
enableClientValidation | boolean | 鏄惁鎵ц瀹㈡埛绔獙璇併傞粯璁ゅ间负true銆 鍙傝CActiveForm::enableClientValidation浠ヤ簡瑙f洿澶氬叧浜庡鎴风楠岃瘉鐨勭粏鑺傘 | CValidator |
message | string | 鐢ㄦ埛鑷畾涔夌殑閿欒鎻愮ず淇℃伅銆備笉鍚岀殑楠岃瘉鍣ㄥ彲浠ュ湪璇ヤ俊鎭腑 瀹氫箟鍚勭鍗犱綅绗︼紙灏嗚瀹為檯鍊兼浛鎹級銆傚崰浣嶇鈥渰attribute}鈥濆彲浠ヨ鎵鏈 楠岃瘉鍣ㄨ瘑鍒紝瀹冧細琚娇鐢ㄥ睘鎬х殑鏍囩鏉ユ浛鎹€ | CValidator |
on | array | 楠岃瘉鍣ㄥ皢琚簲鐢ㄥ埌鐨勬儏鏅ā寮忕殑鍒楄〃銆 鏁扮粍鐨勯敭-鍊奸兘鏄儏鏅ā寮忕殑鍚嶇О銆 | CValidator |
operator | string | 鐢ㄤ簬姣旇緝鐨勬搷浣滅銆傞粯璁ゅ兼槸鈥=鈥濄
涓嬪垪鎿嶄綔绗︽槸鏈夋晥鐨勶細
|
CCompareValidator |
safe | boolean | 杩涜鏁村潡璧嬪兼槸鏄惁鑰冭檻姝ら獙璇佸櫒涓垪鍑虹殑灞炴х殑瀹夊叏鎬с 榛樿鍊间负true銆 | CValidator |
skipOnError | boolean | 濡傛灉褰撳墠灞炴у凡缁忓瓨鍦ㄩ獙璇侀敊璇紝杩欎釜楠岃瘉瑙勫垯 鏄惁璺宠繃銆傞粯璁ゅ兼槸false銆 | CValidator |
strict | boolean | 鏄惁鎵ц涓ユ牸鐨勬瘮杈冿紙鍊间笌绫诲瀷閮藉繀椤诲畬鍏ㄤ竴鑷达級 榛樿鍊间负false銆 | CCompareValidator |
鍏叡鏂规硶
鍙椾繚鎶ゆ柟娉
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
addError() | 娣诲姞鍏充簬鎸囧畾灞炴х殑涓涓敊璇彁绀轰俊鎭埌娲诲姩璁板綍涓 | CValidator |
isEmpty() | 妫娴嬬粰瀹氬兼槸鍚︿负绌恒 | CValidator |
validateAttribute() | 楠岃瘉浼犲叆瀵硅薄鐨勫睘鎬с | CCompareValidator |
灞炴ц缁
allowEmpty
灞炴
public boolean $allowEmpty;
姝ゅ睘鎬у兼槸鍚﹀彲浠ヤ负null鎴杄mpty銆傞粯璁ゅ间负false銆 濡傛灉璁剧疆涓簍rue锛岃〃绀哄綋瀹冧负绌烘椂姝ゅ睘鎬т篃浼氶氳繃楠岃瘉銆
compareAttribute
灞炴
public string $compareAttribute;
鐢ㄤ簬姣旇緝鐨勫睘鎬х殑鍚嶅瓧銆
compareValue
灞炴
public string $compareValue;
鐢ㄤ簬姣旇緝鐨勫父閲忓
operator
灞炴
public string $operator;
鐢ㄤ簬姣旇緝鐨勬搷浣滅銆傞粯璁ゅ兼槸鈥=鈥濄 涓嬪垪鎿嶄綔绗︽槸鏈夋晥鐨勶細
- '=' 鎴 '==': 楠岃瘉涓や釜鍊兼槸鍚︾浉绛夈傚鏋strict璁剧疆涓簍rue锛 灏嗕細浣跨敤涓ユ牸妯″紡杩涜姣旇緝锛堝嵆鍚屾椂姣旇緝绫诲瀷鍜屽硷級銆
- '!=': 楠岃瘉涓や釜鍊兼槸鍚︿笉绛夈傚鏋strict璁剧疆涓簍rue锛 灏嗕細浣跨敤涓ユ牸妯″紡杩涜姣旇緝锛堝嵆鍚屾椂姣旇緝绫诲瀷鍜屽硷級銆
- '>': 楠岃瘉闇瑕佹娴嬬殑灞炴х殑鍊兼槸鍚﹀ぇ浜庣敤鏉ユ瘮杈冪殑鍊笺
- '>=': 楠岃瘉闇瑕佹娴嬬殑灞炴х殑鍊兼槸鍚﹀ぇ浜庢垨绛変簬鐢ㄦ潵姣旇緝鐨勫笺
- '<': 楠岃瘉闇瑕佹娴嬬殑灞炴х殑鍊兼槸鍚﹀皬浜庣敤鏉ユ瘮杈冪殑鍊笺
- '<=': 楠岃瘉闇瑕佹娴嬬殑灞炴х殑鍊兼槸鍚﹀皬浜庢垨绛変簬鐢ㄦ潵姣旇緝鐨勫笺
strict
灞炴
public boolean $strict;
鏄惁鎵ц涓ユ牸鐨勬瘮杈冿紙鍊间笌绫诲瀷閮藉繀椤诲畬鍏ㄤ竴鑷达級 榛樿鍊间负false銆
鏂规硶璇︾粏
clientValidateAttribute()
鏂规硶
锛堝彲鐢ㄨ嚜 v1.1.7锛
public string clientValidateAttribute(CModel $object, string $attribute)
| ||
$object | CModel | 闇瑕侀獙璇佺殑鏁版嵁瀵硅薄 |
$attribute | string | 闇瑕侀獙璇佺殑灞炴у悕瀛 |
{return} | string | 瀹㈡埛绔獙璇佽剼鏈 |
婧愮爜锛 framework/validators/CCompareValidator.php#153 (鏄剧ず)
public function clientValidateAttribute($object,$attribute)
{
if($this->compareValue !== null)
{
$compareTo=$this->compareValue;
$compareValue=CJSON::encode($this->compareValue);
}
else
{
$compareAttribute=$this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
$compareValue="\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()";
$compareTo=$object->getAttributeLabel($compareAttribute);
}
$message=$this->message;
switch($this->operator)
{
case '=':
case '==':
if($message===null)
$message=Yii::t('yii','{attribute} must be repeated exactly.');
$condition='value!='.$compareValue;
break;
case '!=':
if($message===null)
$message=Yii::t('yii','{attribute} must not be equal to "{compareValue}".');
$condition='value=='.$compareValue;
break;
case '>':
if($message===null)
$message=Yii::t('yii','{attribute} must be greater than "{compareValue}".');
$condition='parseFloat(value)<=parseFloat('.$compareValue.')';
break;
case '>=':
if($message===null)
$message=Yii::t('yii','{attribute} must be greater than or equal to "{compareValue}".');
$condition='parseFloat(value)<parseFloat('.$compareValue.')';
break;
case '<':
if($message===null)
$message=Yii::t('yii','{attribute} must be less than "{compareValue}".');
$condition='parseFloat(value)>=parseFloat('.$compareValue.')';
break;
case '<=':
if($message===null)
$message=Yii::t('yii','{attribute} must be less than or equal to "{compareValue}".');
$condition='parseFloat(value)>parseFloat('.$compareValue.')';
break;
default:
throw new CException(Yii::t('yii','Invalid operator "{operator}".',array('{operator}'=>$this->operator)));
}
$message=strtr($message,array(
'{attribute}'=>$object->getAttributeLabel($attribute),
'{compareValue}'=>$compareTo,
));
return "
if(".($this->allowEmpty ? "$.trim(value)!='' && " : '').$condition.") {
messages.push(".CJSON::encode($message).");
}
";
}
杩斿洖鎵ц瀹㈡埛绔獙璇佹墍闇鐨凧avaScript銆
validateAttribute()
鏂规硶
protected void validateAttribute(CModel $object, string $attribute)
| ||
$object | CModel | 闇瑕侀獙璇佺殑瀵硅薄 |
$attribute | string | 闇瑕侀獙璇佺殑灞炴 |
婧愮爜锛 framework/validators/CCompareValidator.php#81 (鏄剧ず)
protected function validateAttribute($object,$attribute)
{
$value=$object->$attribute;
if($this->allowEmpty && $this->isEmpty($value))
return;
if($this->compareValue!==null)
$compareTo=$compareValue=$this->compareValue;
else
{
$compareAttribute=$this->compareAttribute===null ? $attribute.'_repeat' : $this->compareAttribute;
$compareValue=$object->$compareAttribute;
$compareTo=$object->getAttributeLabel($compareAttribute);
}
switch($this->operator)
{
case '=':
case '==':
if(($this->strict && $value!==$compareValue) || (!$this->strict && $value!=$compareValue))
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be repeated exactly.');
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo));
}
break;
case '!=':
if(($this->strict && $value===$compareValue) || (!$this->strict && $value==$compareValue))
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must not be equal to "{compareValue}".');
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo,'{compareValue}'=>$compareValue));
}
break;
case '>':
if($value<=$compareValue)
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be greater than "{compareValue}".');
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo,'{compareValue}'=>$compareValue));
}
break;
case '>=':
if($value<$compareValue)
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be greater than or equal to "{compareValue}".');
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo,'{compareValue}'=>$compareValue));
}
break;
case '<':
if($value>=$compareValue)
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be less than "{compareValue}".');
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo,'{compareValue}'=>$compareValue));
}
break;
case '<=':
if($value>$compareValue)
{
$message=$this->message!==null?$this->message:Yii::t('yii','{attribute} must be less than or equal to "{compareValue}".');
$this->addError($object,$attribute,$message,array('{compareAttribute}'=>$compareTo,'{compareValue}'=>$compareValue));
}
break;
default:
throw new CException(Yii::t('yii','Invalid operator "{operator}".',array('{operator}'=>$this->operator)));
}
}
楠岃瘉浼犲叆瀵硅薄鐨勫睘鎬с 濡傛灉楠岃瘉鍒颁换浣曢敊璇紝閿欒娑堟伅灏嗚娣诲姞鍒拌瀵硅薄涓