CCaptchaValidator
CCaptchaValidator validates that the attribute value is the same as the verification code displayed in the CAPTCHA.
CCaptchaValidator should be used together with
CCaptchaAction.
Public Properties
Property |
Type |
Description |
Defined By |
allowEmpty | boolean | whether the attribute value can be null or empty. | CCaptchaValidator |
attributes | array | list of attributes to be validated. | CValidator |
builtInValidators | array | list of built-in validators (name=>class) | CValidator |
caseSensitive | boolean | whether the comparison is case sensitive. | CCaptchaValidator |
enableClientValidation | boolean | whether to perform client-side validation. | CValidator |
except | array | list of scenarios that the validator should not be applied to. | CValidator |
message | string | the user-defined error message. | CValidator |
on | array | list of scenarios that the validator should be applied. | CValidator |
safe | boolean | whether attributes listed with this validator should be considered safe for massive assignment. | CValidator |
skipOnError | boolean | whether this validation rule should be skipped when there is already a validation error for the current attribute. | CValidator |
Property Details
public boolean $allowEmpty;
whether the attribute value can be null or empty. Defaults to false, meaning the attribute is invalid if it is empty.
protected CCaptchaAction getCaptchaAction()
Returns the CAPTCHA action object.
public boolean $caseSensitive;
whether the comparison is case sensitive. Defaults to false.
Method Details
public string clientValidateAttribute(CModel $object, string $attribute) |
$object | CModel | the data object being validated |
$attribute | string | the name of the attribute to be validated. |
{return} | string | the client-side validation script. |
Source Code: framework/validators/CCaptchaValidator.php#91 (
show)
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 = jQuery('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(jQuery.trim(value)!='') {
$js
}
";
}
return $js;
}
Returns the JavaScript needed for performing client-side validation.
Source Code: framework/validators/CCaptchaValidator.php#64 (
show)
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;
}
Returns the CAPTCHA action object.
protected void validateAttribute(CModel $object, string $attribute) |
$object | CModel | the object being validated |
$attribute | string | the attribute being validated |
Source Code: framework/validators/CCaptchaValidator.php#44 (
show)
protected function validateAttribute($object,$attribute)
{
$value=$object->$attribute;
if($this->allowEmpty && $this->isEmpty($value))
return;
$captcha=$this->getCaptchaAction();
// reason of array checking is explained here: https://github.com/yiisoft/yii/issues/1955
if(is_array($value) || !$captcha->validate($value,$this->caseSensitive))
{
$message=$this->message!==null?$this->message:Yii::t('yii','The verification code is incorrect.');
$this->addError($object,$attribute,$message);
}
}
Validates the attribute of the object. If there is any error, the error message is added to the object.