Yii Framework v1.1.10 绫诲弬鑰

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杩樺厑璁告寚瀹氫笅鍒楀崰浣嶇锛
  • {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 鐢ㄤ簬姣旇緝鐨勬搷浣滅銆傞粯璁ゅ兼槸鈥=鈥濄 涓嬪垪鎿嶄綔绗︽槸鏈夋晥鐨勶細
  • '=' 鎴 '==': 楠岃瘉涓や釜鍊兼槸鍚︾浉绛夈傚鏋strict璁剧疆涓簍rue锛 灏嗕細浣跨敤涓ユ牸妯″紡杩涜姣旇緝锛堝嵆鍚屾椂姣旇緝绫诲瀷鍜屽硷級銆
  • '!=': 楠岃瘉涓や釜鍊兼槸鍚︿笉绛夈傚鏋strict璁剧疆涓簍rue锛 灏嗕細浣跨敤涓ユ牸妯″紡杩涜姣旇緝锛堝嵆鍚屾椂姣旇緝绫诲瀷鍜屽硷級銆
  • '>': 楠岃瘉闇瑕佹娴嬬殑灞炴х殑鍊兼槸鍚﹀ぇ浜庣敤鏉ユ瘮杈冪殑鍊笺
  • '>=': 楠岃瘉闇瑕佹娴嬬殑灞炴х殑鍊兼槸鍚﹀ぇ浜庢垨绛変簬鐢ㄦ潵姣旇緝鐨勫笺
  • '<': 楠岃瘉闇瑕佹娴嬬殑灞炴х殑鍊兼槸鍚﹀皬浜庣敤鏉ユ瘮杈冪殑鍊笺
  • '<=': 楠岃瘉闇瑕佹娴嬬殑灞炴х殑鍊兼槸鍚﹀皬浜庢垨绛変簬鐢ㄦ潵姣旇緝鐨勫笺
CCompareValidator
safe boolean 杩涜鏁村潡璧嬪兼槸鏄惁鑰冭檻姝ら獙璇佸櫒涓垪鍑虹殑灞炴х殑瀹夊叏鎬с 榛樿鍊间负true銆 CValidator
skipOnError boolean 濡傛灉褰撳墠灞炴у凡缁忓瓨鍦ㄩ獙璇侀敊璇紝杩欎釜楠岃瘉瑙勫垯 鏄惁璺宠繃銆傞粯璁ゅ兼槸false銆 CValidator
strict boolean 鏄惁鎵ц涓ユ牸鐨勬瘮杈冿紙鍊间笌绫诲瀷閮藉繀椤诲畬鍏ㄤ竴鑷达級 榛樿鍊间负false銆 CCompareValidator

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
applyTo() 杩斿洖鍊兼寚瀹氭楠岃瘉鍣ㄦ槸鍚﹀簲鐢ㄥ埌鎸囧畾鐨勬儏鏅ā寮忋 CValidator
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
clientValidateAttribute() 杩斿洖鎵ц瀹㈡埛绔獙璇佹墍闇鐨凧avaScript銆 CCompareValidator
createValidator() 鍒涘缓涓涓獙璇佸櫒瀵硅薄銆 CValidator
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
validate() 楠岃瘉缁欏畾鐨勫璞° CValidator

鍙椾繚鎶ゆ柟娉

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
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 灞炴
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)));
    }
}

楠岃瘉浼犲叆瀵硅薄鐨勫睘鎬с 濡傛灉楠岃瘉鍒颁换浣曢敊璇紝閿欒娑堟伅灏嗚娣诲姞鍒拌瀵硅薄涓

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.