Yii Framework v1.1.10 绫诲弬鑰

CCaptchaValidator

system.validators
缁ф壙 class CCaptchaValidator » CValidator » CComponent
婧愯嚜 1.0
鐗堟湰 $Id: CCaptchaValidator.php 3124 2011-03-25 15:48:05Z qiang.xue $
婧愮爜 framework/validators/CCaptchaValidator.php
CCaptchaValidator 鐢ㄤ簬楠岃瘉灞炴у煎拰楠岃瘉鐮佷腑鏄剧ず鐨勪竴鑷淬

CCaptchaValidator 蹇呴』鍜CCaptchaAction涓璧蜂娇鐢ㄣ

鍏叡灞炴

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
allowEmpty boolean 灞炴у兼椂鍊欏彲浠ヤ负null鎴栬 empty銆 榛樿鏄痜alse锛岃〃绀哄綋鍊间负鏄痚mpty鏃讹紝灞炴ф槸鏃犳晥鐨勩 CCaptchaValidator
attributes array 闇瑕佽楠岃瘉鐨勫睘鎬х殑鍒楄〃銆 CValidator
builtInValidators array 鍐呯疆楠岃瘉鍣ㄥ垪琛 (name=>class) CValidator
caseSensitive boolean 姣旇緝鏄惁鍖哄垎澶у皬鍐欍傞粯璁ゆ槸false锛岃〃绀轰笉鍖哄垎澶у皬鍐欍 CCaptchaValidator
enableClientValidation boolean 鏄惁鎵ц瀹㈡埛绔獙璇併傞粯璁ゅ间负true銆 鍙傝CActiveForm::enableClientValidation浠ヤ簡瑙f洿澶氬叧浜庡鎴风楠岃瘉鐨勭粏鑺傘 CValidator
message string 鐢ㄦ埛鑷畾涔夌殑閿欒鎻愮ず淇℃伅銆備笉鍚岀殑楠岃瘉鍣ㄥ彲浠ュ湪璇ヤ俊鎭腑 瀹氫箟鍚勭鍗犱綅绗︼紙灏嗚瀹為檯鍊兼浛鎹級銆傚崰浣嶇鈥渰attribute}鈥濆彲浠ヨ鎵鏈 楠岃瘉鍣ㄨ瘑鍒紝瀹冧細琚娇鐢ㄥ睘鎬х殑鏍囩鏉ユ浛鎹€ CValidator
on array 楠岃瘉鍣ㄥ皢琚簲鐢ㄥ埌鐨勬儏鏅ā寮忕殑鍒楄〃銆 鏁扮粍鐨勯敭-鍊奸兘鏄儏鏅ā寮忕殑鍚嶇О銆 CValidator
safe boolean 杩涜鏁村潡璧嬪兼槸鏄惁鑰冭檻姝ら獙璇佸櫒涓垪鍑虹殑灞炴х殑瀹夊叏鎬с 榛樿鍊间负true銆 CValidator
skipOnError boolean 濡傛灉褰撳墠灞炴у凡缁忓瓨鍦ㄩ獙璇侀敊璇紝杩欎釜楠岃瘉瑙勫垯 鏄惁璺宠繃銆傞粯璁ゅ兼槸false銆 CValidator

鍙椾繚鎶ゅ睘鎬

闅愯棌缁ф壙灞炴

灞炴绫诲瀷鎻忚堪瀹氫箟鍦
captchaAction CCaptchaAction 杩斿洖楠岃瘉鐮佺殑action瀵硅薄銆 CCaptchaValidator

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__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() 杩斿洖鐢ㄤ簬瀹㈡埛绔獙璇佺殑JavaScript銆 CCaptchaValidator
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
getCaptchaAction() 杩斿洖楠岃瘉鐮佺殑action瀵硅薄銆 CCaptchaValidator
isEmpty() 妫娴嬬粰瀹氬兼槸鍚︿负绌恒 CValidator
validateAttribute() 楠岃瘉闇瑕佽楠岃瘉鐨勫璞$殑灞炴с CCaptchaValidator

灞炴ц缁

allowEmpty 灞炴
public boolean $allowEmpty;

灞炴у兼椂鍊欏彲浠ヤ负null鎴栬 empty銆 榛樿鏄痜alse锛岃〃绀哄綋鍊间负鏄痚mpty鏃讹紝灞炴ф槸鏃犳晥鐨勩

captchaAction 灞炴 鍙 锛堝彲鐢ㄨ嚜 v1.1.7锛

杩斿洖楠岃瘉鐮佺殑action瀵硅薄銆

caseSensitive 灞炴
public boolean $caseSensitive;

姣旇緝鏄惁鍖哄垎澶у皬鍐欍傞粯璁ゆ槸false锛岃〃绀轰笉鍖哄垎澶у皬鍐欍

鏂规硶璇︾粏

clientValidateAttribute() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.7锛
public string clientValidateAttribute(CModel $object, string $attribute)
$object CModel 琚獙璇佺殑鏁版嵁銆
$attribute string 闇瑕佽楠岃瘉鐨勫睘鎬х殑鍚嶅瓧銆
{return} string 瀹㈡埛绔獙璇佽剼鏈
婧愮爜锛 framework/validators/CCaptchaValidator.php#90 (鏄剧ず)
public function clientValidateAttribute($object,$attribute)
{
    
$captcha=$this->getCaptchaAction();
    
$message=$this->message!==null $this->message Yii::t('yii','The verification code is incorrect.');
    
$message=strtr($message, array(
        
'{attribute}'=>$object->getAttributeLabel($attribute),
    ));
    
$code=$captcha->getVerifyCode(false);
    
$hash=$captcha->generateValidationHash($this->caseSensitive $code strtolower($code));
    
$js="
var hash = $('body').data('
{$this->captchaAction}.hash');
if (hash == null)
hash = 
$hash;
else
hash = hash["
.($this->caseSensitive 1)."];
for(var i=value.length-1, h=0; i >= 0; --i) h+=value."
.($this->caseSensitive '' 'toLowerCase().')."charCodeAt(i);
if(h != hash) {
messages.push("
.CJSON::encode($message).");
}
"
;

    if(
$this->allowEmpty)
    {
        
$js="
if($.trim(value)!='') {
$js
}
"
;
    }

    return 
$js;
}

杩斿洖鐢ㄤ簬瀹㈡埛绔獙璇佺殑JavaScript銆

getCaptchaAction() 鏂规硶 锛堝彲鐢ㄨ嚜 v1.1.7锛
protected CCaptchaAction getCaptchaAction()
{return} CCaptchaAction action瀵硅薄
婧愮爜锛 framework/validators/CCaptchaValidator.php#63 (鏄剧ず)
protected function getCaptchaAction()
{
    if((
$captcha=Yii::app()->getController()->createAction($this->captchaAction))===null)
    {
        if(
strpos($this->captchaAction,'/')!==false// contains controller or module
        
{
            if((
$ca=Yii::app()->createController($this->captchaAction))!==null)
            {
                list(
$controller,$actionID)=$ca;
                
$captcha=$controller->createAction($actionID);
            }
        }
        if(
$captcha===null)
            throw new 
CException(Yii::t('yii','CCaptchaValidator.action "{id}" is invalid. Unable to find such an action in the current controller.',
                    array(
'{id}'=>$this->captchaAction)));
    }
    return 
$captcha;
}

杩斿洖楠岃瘉鐮佺殑action瀵硅薄銆

validateAttribute() 鏂规硶
protected void validateAttribute(CModel $object, string $attribute)
$object CModel 闇瑕佽楠岃瘉瀵硅薄銆
$attribute string 闇瑕佽楠岃瘉鐨勫睘鎬
婧愮爜锛 framework/validators/CCaptchaValidator.php#45 (鏄剧ず)
protected function validateAttribute($object,$attribute)
{
    
$value=$object->$attribute;
    if(
$this->allowEmpty && $this->isEmpty($value))
        return;
    
$captcha=$this->getCaptchaAction();
    if(!
$captcha->validate($value,$this->caseSensitive))
    {
        
$message=$this->message!==null?$this->message:Yii::t('yii','The verification code is incorrect.');
        
$this->addError($object,$attribute,$message);
    }
}

楠岃瘉闇瑕佽楠岃瘉鐨勫璞$殑灞炴с 濡傛灉杩欓噷鍑虹幇浠讳綍閿欒锛岄偅涔堣繖涓敊璇俊鎭皢琚坊鍔犲埌楠岃瘉瀵硅薄銆

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