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

  1. Requirements
  2. Usage

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.

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

4 0
Yii Version: 1.1
License: BSD-2-Clause
Category: Validation
Developed by: hightman
Created on: Mar 29, 2013
Last updated: 9 years ago


show all

Related Extensions