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 |
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
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 |
鍏叡鏂规硶
鍙椾繚鎶ゆ柟娉
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
addError() | 娣诲姞鍏充簬鎸囧畾灞炴х殑涓涓敊璇彁绀轰俊鎭埌娲诲姩璁板綍涓 | CValidator |
getCaptchaAction() | 杩斿洖楠岃瘉鐮佺殑action瀵硅薄銆 | CCaptchaValidator |
isEmpty() | 妫娴嬬粰瀹氬兼槸鍚︿负绌恒 | CValidator |
validateAttribute() | 楠岃瘉闇瑕佽楠岃瘉鐨勫璞$殑灞炴с | CCaptchaValidator |
灞炴ц缁
allowEmpty
灞炴
public boolean $allowEmpty;
灞炴у兼椂鍊欏彲浠ヤ负null鎴栬 empty銆 榛樿鏄痜alse锛岃〃绀哄綋鍊间负鏄痚mpty鏃讹紝灞炴ф槸鏃犳晥鐨勩
captchaAction
灞炴
鍙 锛堝彲鐢ㄨ嚜 v1.1.7锛
protected CCaptchaAction getCaptchaAction()
杩斿洖楠岃瘉鐮佺殑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 ? 0 : 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);
}
}
楠岃瘉闇瑕佽楠岃瘉鐨勫璞$殑灞炴с 濡傛灉杩欓噷鍑虹幇浠讳綍閿欒锛岄偅涔堣繖涓敊璇俊鎭皢琚坊鍔犲埌楠岃瘉瀵硅薄銆