Like Google user login, first to show captcha after several times of wrong validation, and hide it next several times once pass the validation of captcha.
This is a behavior extension for captcha of CModel validation, make the captcha validator works like the above description.
Requirements ¶
Yii-1.1.x or above
Usage ¶
Put the downloaded file 'SmartCaptchaBehavior.php' to 'application.components' or other place can autoloaded.
Let's use LoginForm as an example:
Modify application.model.LoginForm
¶
// 1. rules method of LoginForm (model)
public function rules()
{
return array(
// put the captcha validator first
array('verifyCode', 'captcha'),
array('username, password', 'required'),
array('rememberMe', 'boolean'),
// password authentication, recommend to add SkipOnError
array('password', 'authenticate', 'skipOnError' => true),
);
}
// 2. attach the behavior to LoginForm
public function behaviors()
{
return array(
'smartCaptcha' => array(
'class' => 'SmartCaptchaBehavior',
'numErrorBefore' => 2, // the number of errors allowed before first to show captcha.
'numErrorAfter' => 5, // the number of errors allowed once pass captcha validation.
'attributes' => null, // list of attributes whose error affects to show captcha. Defaults to null for all attributes.
),
);
}
Modify application.views.site.login
¶
[html]
... other codes ...
<?php if($model->getIsNeedCaptcha()): ?>
<!-- ... render captcha input box and image here ... -->
<?php endif; ?>
... other codes ....
Would you please clarify what I should place instead of this:
reply to #13020
The following code works with YiiBooster:
<?php if ($model->getIsNeedCaptcha()) echo $form->captchaRow($model, 'verifyCode'); ?>
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.