Yii 1.1: smartcaptcha

More humanized for captcha, just show after some times error and hide once passed

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.


Yii-1.1.x or above


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

... other codes ... 
<?php if($model->getIsNeedCaptcha()): ?>
    <!-- ... render captcha input box and image here ... -->
<?php endif; ?>
... other codes ....

Total 2 comments

#14458 report it
hightman at 2013/08/13 08:18am
reply to #13020

The following code works with YiiBooster:

<?php if ($model->getIsNeedCaptcha()) echo $form->captchaRow($model, 'verifyCode'); ?>
#13020 report it
wwwwww at 2013/04/29 02:05am
<!-- ... render captcha input box and image here ... -->

Would you please clarify what I should place instead of this:

Leave a comment

Please to leave your comment.

Create extension