CCaptchaValidator

优质
小牛编辑
125浏览
2023-12-01
所有包 | 属性 | 方法
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一起使用。

公共属性

隐藏继承属性

属性类型描述定义在
allowEmptyboolean属性值时候可以为null或者 empty。 默认是false,表示当值为是empty时,属性是无效的。CCaptchaValidator
attributesarray需要被验证的属性的列表。CValidator
builtInValidatorsarray内置验证器列表 (name=>class)CValidator
caseSensitiveboolean比较是否区分大小写。默认是false,表示不区分大小写。CCaptchaValidator
enableClientValidationboolean是否执行客户端验证。默认值为true。 参见CActiveForm::enableClientValidation以了解更多关于客户端验证的细节。CValidator
messagestring用户自定义的错误提示信息。不同的验证器可以在该信息中 定义各种占位符(将被实际值替换)。占位符“{attribute}”可以被所有 验证器识别,它会被使用属性的标签来替换。CValidator
onarray验证器将被应用到的情景模式的列表。 数组的键-值都是情景模式的名称。CValidator
safeboolean进行整块赋值是是否考虑此验证器中列出的属性的安全性。 默认值为true。CValidator
skipOnErrorboolean如果当前属性已经存在验证错误,这个验证规则 是否跳过。默认值是false。CValidator

受保护属性

隐藏继承属性

属性类型描述定义在
captchaActionCCaptchaAction返回验证码的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()计算一个PHP表达式,或根据组件上下文执行回调。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。 默认是false,表示当值为是empty时,属性是无效的。

captchaAction 属性 只读 (可用自 v1.1.7) protected CCaptchaAction getCaptchaAction()

返回验证码的action对象。

caseSensitive 属性 public boolean $caseSensitive;

比较是否区分大小写。默认是false,表示不区分大小写。

方法详细

clientValidateAttribute() 方法 (可用自 v1.1.7)
public string clientValidateAttribute(CModel $object, string $attribute)
$objectCModel被验证的数据。
$attributestring需要被验证的属性的名字。
{return}string客户端验证脚本。
源码: framework/validators/CCaptchaValidator.php#90 (显示) publicfunctionclientValidateAttribute($object,$attribute)
{
$captcha=$this->getCaptchaAction();
$message=$this->message!==null?$this->message:Yii::t('yii','Theverificationcodeisincorrect.');
$message=strtr($message,array(
'{attribute}'=>$object->getAttributeLabel($attribute),
));
$code=$captcha->getVerifyCode(false);
$hash=$captcha->generateValidationHash($this->caseSensitive?$code:strtolower($code));
$js="
varhash=$('body').data('{$this->captchaAction}.hash');
if(hash==null)
hash=$hash;
else
hash=hash[".($this->caseSensitive?0:1)."];
for(vari=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。

参见

  • CActiveForm::enableClientValidation
getCaptchaAction() 方法 (可用自 v1.1.7)
protected CCaptchaAction getCaptchaAction()
{return}CCaptchaActionaction对象
源码: framework/validators/CCaptchaValidator.php#63 (显示) protectedfunctiongetCaptchaAction()
{
if(($captcha=Yii::app()->getController()->createAction($this->captchaAction))===null)
{
if(strpos($this->captchaAction,'/')!==false)//containscontrollerormodule
{
if(($ca=Yii::app()->createController($this->captchaAction))!==null)
{
list($controller,$actionID)=$ca;
$captcha=$controller->createAction($actionID);
}
}
if($captcha===null)
thrownewCException(Yii::t('yii','CCaptchaValidator.action"{id}"isinvalid.Unabletofindsuchanactioninthecurrentcontroller.',
array('{id}'=>$this->captchaAction)));
}
return$captcha;
}

返回验证码的action对象。

validateAttribute() 方法
protected void validateAttribute(CModel $object, string $attribute)
$objectCModel需要被验证对象。
$attributestring需要被验证的属性
源码: framework/validators/CCaptchaValidator.php#45 (显示) protectedfunctionvalidateAttribute($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','Theverificationcodeisincorrect.');
$this->addError($object,$attribute,$message);
}
}

验证需要被验证的对象的属性。 如果这里出现任何错误,那么这个错误信息将被添加到验证对象。