Just learned how to add captcha to a form using Yii framework. Followed the steps specified in the Yii Book, but was not working and needed a special configuration [found in a forum post]. Here is how you add a Captcha to existing form :
Model
Add a field in the form (model) which is used to verify the captcha code
<?php class User extends CActiveRecord { public $verifyCode; //other fields public function rules() { return array( //my other rules array('verifyCode', 'captcha', 'allowEmpty' => ! CCaptcha::checkRequirements()), ); } public function attributeLabels() { return array( //my other element's labels 'verifyCode' => 'Enter the text in image' ); } }
View
<div class="row"> <p> <?php echo CHtml::activeLabelEx($model, 'verifyCode'); ?> <?php $this->widget('CCaptcha'); ?> </p> <p> <?php echo CHtml::activeTextField($model, 'verifyCode'); ?> <?php echo CHtml::error($model, 'verifyCode'); ?> </p> </div>
Controller
public class UserController extends CController { public function actions() { return array ('captcha' => array('class' => 'CCaptchaAction', 'backColor' => 0xFFFFFF)); } public function accessRules() { return array( array('allow', // allow all users to perform 'index' and 'view' actions 'actions'=>array('register', 'index','view', 'login', 'captcha'), 'users'=>array('*'), ), // other rules ); } }
References :
http://www.yiiframework.com/forum/index.php?/topic/23660-yii-captcha-doesnt-work/