How to show a Captcha in CForm?

You are viewing revision #2 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#3) »

In this wiki I will show how could use a Captcha in yii CForm. The easy way to show captcha image is create a form using CHtml method & CActiveForm, but CForm also should be able to show a captcha.

In your components -

Create a widget MyCaptcha.php

class MyCaptcha extends CCaptcha
{
   public $model;
   public $attribute;
                
   public function run(){
	   
       parent::run();
       echo CHtml::activeTextField($this->model, $this->attribute);
    }
}

Add this into your contoller, to show the captcha image.

public function actions()
 {
   return array(
	  'captcha'=>array(
	  'class'=>'CCaptchaAction',
	  'backColor'=>0xFFFFFF,
	 ),
   );
}

in view, your CForm like -

return array(
    'title'=>'Please provide your login credential',
 
    'elements'=>array(
        'username'=>array(
            'type'=>'text',
            'maxlength'=>32,
        ),
        'password'=>array(
            'type'=>'password',
            'maxlength'=>32,
        ),
        'verifyCode'=>array(
	    'type'=>'MyCaptcha',   //render the captcha image & text field.
            'showRefreshButton'=>false,
	    'clickableImage'=>true,
	 ),
    ),
 
    'buttons'=>array(
        'login'=>array(
            'type'=>'submit',
            'label'=>'Login',
        ),
    ),
);

Try this way it's working fine :)..

0 0
3 followers
Viewed: 19 008 times
Version: Unknown (update)
Category: How-tos
Written by: Rohit Suthar
Last updated by: Rohit Suthar
Created on: Jul 17, 2014
Last updated: 10 years ago
Update Article

Revisions

View all history

Related Articles