How to show a Captcha in CForm?

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(){
       echo CHtml::activeTextField($this->model, $this->attribute);

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

public function actions()
   return array(

in view, your CForm like -

return array(
    'title'=>'Please provide your login credential',
	          'type'=>'MyCaptcha',   //render the captcha image & text field.

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

0 0
Viewed: 10 671 times
Version: 1.1
Category: How-tos
Written by: Rohit Suthar
Last updated by: Rohit Suthar
Created on: Jul 17, 2014
Last updated: 3 years ago
Update Article


View all history

Related Articles