CCaptcha Validation

Hi,

I try to do CCaptcha Validation but it always return me true even it is wrong.

I have this code in my view file


<?php $this->widget('CCaptcha', array(

	    'captchaAction'=>'captcha',

	    'showRefreshButton'=>true,

		'id'=>'ticketCaptcha'

		));

	?>

<?php echo CHtml::textField('verifyCode'); ?>

this is in my modele


public function rules()

	{

		return array(

			array('verifyCode', 'captcha', 'on'=>'insert', 'allowEmpty'=>!Yii::app()->user->isGuest || !extension_loaded('gd')),

		);

	}

and this is in my controller


$site=new site();

      $site->verifyCode=$_POST['verifyCode'];

      echo $site->validate();

Is there any advice, what i am doing is wrong ?

Your validation rule is set only to the scenario ‘insert’ so you would need to do something like this :




$site=new site('insert');

$site->verifyCode=$_POST['verifyCode'];

echo $site->validate();



It didn’t make difference. Actually I am not inserting or updating database. The form just send email.

Calling validate() still checks against your validation rules.

There are a few things to check.

  1. have you specified the $verifyCode public property in your model?

  2. Do you have a beforeValidate() method in your model?

  3. Instead of echoing ‘$site->validate()’ what is the output of ’ print_r($site->getErrors()); ’

eg:




$site=new site('insert');

$site->verifyCode=$_POST['verifyCode'];

$site->validate();

print_r($site->getErrors());



$site->getErrors() returns empty array.

What should i have inside beforeValidate() function. I just add like this


protected function beforeValidate()

	{

		return true;

	}

Do you require the beforeValidate() method? If so you need to invoke the parent method. If not you should remove the method from your model class.





protected function beforeValidate()

{

   ............


   return parent::beforeValidate();

}



I don’t really require this method. But even I invoke the parent method, it returns same. There is no error, even though i entered wrong character to test.

Any help please

mmm… running out of ideas here :)

Try dump your session array and see what the value of ‘captcha’ is?

Is it saving into Session ohhh that’s great to know. I didn’t think of this. Thanks it solves my problem

Great that the problem is solved! :)