captcha is not working first time

Captcha is not working correctly, After form load it is not working but if i click redraw captcha and I submit the form works correctly.

This is my captcha code in form

<div class="row">

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

        <div class="user-captcha captcha">

            <?php $this->widget('CCaptcha',array("clickableImage" => TRUE, "buttonLabel" => "<b>Qayta so'rash</b>")); ?>

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

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



In model:

public function rules(){

array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'message' => Yii::t("", "Captchani kiriting.")),

array('verifyCode', 'safe'),...

please help me

I beleive I had similar problem. On testing machine everything was fine, but on production machine problem occured. Yii code was correct, but GD library did not calculate correct coordinates for next captcha letter. I overrided function for captcha image generation.

I’ve seen the same thing too. I haven’t had a chance to debug it deeply, however, I have noticed that the problem goes away in my case if I disable AJAX validation, i.e., set enableAjaxValidation to false for the CActiveForm that the captcha resides within, i.e.,

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(



)); ?>

i have tried your above mention solution …but i need a ajax validation on a form…then how could i sort out this problem.

any one can gimme an idea…

Check this thread for some ideas -

Just to emphasize, those who can afford not to use AJAX validation on the page where you put the captcha, simply avoid AJAX validation for the issue not to happen. I just did so and solved my issue.


I have this problem also, code verification always fails on first try. I tried as mentioned above, my code:

<?php $activeform = $this->beginWidget('CActiveForm', array(


			'action' => 'index.php?r=registration',





// form

<?php if(extension_loaded('gd')): ?>

	<div class="row">

		<?php echo CHtml::activeLabelEx($form,'verifyCode'); ?>


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

		<?php echo CHtml::activeTextField($form,'verifyCode'); ?>


		<p class="hint"><?php echo Yum::t('Please enter the letters as they are shown in the image above.'); ?>

		<br/><?php echo Yum::t('Letters are not case-sensitive.'); ?></p>


	<?php endif; ?>

	<div class="row submit">

		<?php echo CHtml::submitButton(Yum::t('Register')); ?>


<?php $this->endWidget(); ?>

Any further ideas?

It’s simple, use javascript to click in new code onload the page.

Actually, I think this is not 100% correct approach to re-generate captcha on every ajax validation (which Yii does and what leads to continuous misunderstanding among developers). This is why I extended standard CCaptchaValidator to the following simple one.

class AjaxCaptchaValidator extends CCaptchaValidator



   * Skips captcha validation/regeneration during ajax requests


  public $skipAjaxValidation = true;     


   * Validates the attribute of the object.

   * If there is any error, the error message is added to the object.

   * @param CModel $object the object being validated

   * @param string $attribute the attribute being validated


  protected function validateAttribute($object,$attribute)



    && Yii::app()->request->isAjaxRequest) return;





Place it in extensions/validators/AjaxCaptchaValidator.php and use like this:

array('verifyCode', 'ext.validators.AjaxCaptchaValidator', 'allowEmpty' => !Yii::app()->user->isGuest || !CCaptcha::checkRequirements()),

Here the default ‘captcha’ validator is replaced with the new one.

Hope this helps.

I cannot find any documentation that you shouldn’t use CCaptchaValidator in combination with ajaxValidation…

I have the same problem, but how can i separate ajaxValidation form from captcha (with captcha validation ofc)?

Same trouble here. Sometimes it works, sometimes not at all. Can’t find the problem.

By disabling ajax validation and transforming your "ajax needs" with the client validation it works.

Try to put ajax validation to false, and client validation to true.

					'enableAjaxValidation' => false,



else you have other option :

  • Refresh the captcha when you load the page :

$session = Yii::app()->session;

$prefixLen = strlen(CCaptchaAction::SESSION_VAR_PREFIX);

foreach ($session->keys as $key) {

	if (strncmp(CCaptchaAction::SESSION_VAR_PREFIX, $key, $prefixLen) == 0)



If still not working :

/* simulate a click on "refresh captcha" for GET requests */

if (!Yii::app()->request->isPostRequest)



 		'$(".captcha a").trigger("click");',



i just solved the same problem in … the thing is I modified my config file main.php with notepad from windows, then i upload the file and then the problem appears…

For solving it i download the previous version of main.php and modified again but using notepad++ and uploaded the file again…don’t now why…maybe was a codification problem…who knows??

Hope this helps…

Greetings from Cuba.