Yii Framework Forum: CCaptcha with CActiveRecord does not display image - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CCaptcha with CActiveRecord does not display image Rate Topic: -----

#1 User is offline   Collin Lavoie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 22-April 10

Posted 22 April 2010 - 11:26 AM

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.

Attached File(s)


0

#2 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 22 April 2010 - 01:51 PM

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

php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#3 User is offline   Collin Lavoie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 22-April 10

Posted 22 April 2010 - 02:06 PM

View Postjayrulez, on 22 April 2010 - 01:51 PM, said:

<?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
0

#4 User is offline   jayrulez 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 348
  • Joined: 29-July 09

Posted 22 April 2010 - 04:17 PM

View PostCollin Lavoie, on 22 April 2010 - 02:06 PM, said:

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
php:
foreach(array('cat', 'dog', 'cow') as $animal) echo $animal."\n";

python:
[(animal, print(animal)) for animal in ['cat', 'dog', 'cow']]

ruby:
['cat', 'dog', 'cow'].each {|animal| puts animal}


You say Tomato, I say Tomato.
0

#5 User is offline   Collin Lavoie 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 22-April 10

Posted 27 April 2010 - 02:05 PM

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


Quote

2010/04/27 14:58:01 [error] [exception.CHttpException.403] exception 'CHttpException' with message 'You are not authorized to perform this action.' in /opt/yii-1.1.1.r1907/framework/web/auth/CAccessControlFilter.php:153


Granting the permission to all users resolved the issue.

Collin
1

#6 User is offline   arfan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 26-March 14
  • Location:Lahore

Posted 23 May 2014 - 05:10 AM

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('*'),
			),
			
		);
	}

ArfanEmail me
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users