CCaptcha with CActiveRecord does not display image

Hi all,

I require anonymous users to create a contact information record in the database. I also want to provide authorized user the ability to consult and possibly update the data, so I used the crud facility in the yii shell to create a dedicated controller, ActiveRecord model and corresponding views.

Basically, I have copied and adapted the captcha related code from the SiteController, ContactForm model and contact (Contact Us) view to try to replicate the functionality in my "create" action of the Contacts related files (ContactController.php, Contact.php and _form.php, controller, model and view, respectively).

It seems to "work" because there are no runtime errors and the validation process returns a message stating that the verification code is incorrect, yet the image is never displayed.

Here is what I have done:

Controller:


class ContactController extends Controller

{


	/**

	 * Declares class-based actions.

	 */

	public function actions()

	{

		return array(

			// captcha action renders the CAPTCHA image displayed on the contact page

			'captcha'=>array(

				'class'=>'CCaptchaAction',

				'backColor'=>0xFFFFFF,

			),

		);

	}


	<snip>




Model:


class Contact extends CActiveRecord 

{

	/**

	 * The followings are the available columns in table 'Contact':

	 * @var integer $id

	 * @var string $givenname

	 * @var string $surname

	 * @var string $address1

	 * @var string $address2

	 * @var string $city

	 * @var string $zipcode

	 * @var string $email

	 * @var string $website

	 */

	 

	 public $verifyCode;


	<snip>


	public function rules()

	{

		return array(


			<snip>


			// verifyCode needs to be entered correctly

			array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),			

		);

	}


	<snip>




View:




	<snip>


	<?php if(extension_loaded('gd') && $model->isNewRecord): ?>

	<div class="row">

		<?php echo $form->labelEx($model,'verifyCode'); ?>

		<div>

		<?php $this->widget('CCaptcha'); ?>

		<?php echo $form->textField($model,'verifyCode'); ?>

		</div>

		<div class="hint">Please enter the letters as they are shown in the image above.

		<br/>Letters are not case-sensitive.</div>

	</div>

	<?php endif; ?>


	<snip>



I initially suspected that I could not use the CCaptcha widget with an ActiveRecord model, but this does not seem to be so because the "Get a new code" link , which is generated by the widget itself, is displayed (see attached image). Although I do not know how rendering is handled by Yii, I wonder if this is a rendering problem.

If anyone has experienced this issue and found a solution, or if you have some insight that can help me find a solution of my own, I would greatly appreciate your assistance.

Thanks in advance,

Collin Lavoie

Systems Administrator and Developer

SecureOps Inc.




<?php $this->widget('CCaptcha', array('captchaAction'=>'contact/captcha')); ?>



Hi,

Thanks for the reply. Unfortunately, I get the exact same behaviour. :(

I have recently created a new page based on the contact form and having a model extending CFormModel. The captcha image appears correctly.

I strongly suspect that the problem is related to the fact that my contact registration model extends CActiveRecord rather than CFormModel.

Collin

that shouldn’t matter, try this link in your browser and see the result yourproject/?r=contact/captcha

The problem was due to the fact that the “captcha” action was not explicitly allowed in the controller’s accessRules().

Granting the permission to all users resolved the issue.

Collin

Go to AccessRules in your Controller give access to captcha


public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view', 'captcha' ),

				'users'=>array('*'),

			),

			

		);

	}