StreetCoder
StreetCoder

Reputation: 10061

Yii captcha does not work first time

I am trying to create register form where I used captcha. I have enabled ejax validation. But the there is a problem, captcha does not work first time. When I generate new captcha code, that works. My code is:

In model,

public $verifyCode;

public function rules()
{
return array(
    array('verifyCode','captcha','allowEmpty'=>!CCaptcha::checkRequirements(), 'on'=>'registration'),

    array('id, first_name,last_name, email, username, password, password_repeat, user_type,  keystring, status, logo_url, last_login_time, create_time, update_time, validation', 'safe', 'on'=>'search'),
    );
}

In controller:

public function actions()
{
    return array(
        'captcha'=>array(
            'class'=>'CCaptchaAction',
            'backColor'=>0xFFFFFF,
            'testLimit'=>3,
        ),
    );
}

In view:

<?php if(CCaptcha::checkRequirements()): ?>
<div class="row">
    <?php echo $form->labelEx($model,'verifyCode'); ?>
    <?php $this->widget('CCaptcha', array('captchaAction'=>'user/captcha')); ?>
</div>
<div class="formfieldarea">
    <div class="form-text"></div>
    <div class="form-field">
        <?php echo $form->textField($model,'verifyCode'); ?>
        <div class="hint">Please enter the letters as shown.
            <br/>Letters are not case-sensitive.</div>
        <?php echo $form->error($model,'verifyCode'); ?>
    </div>
</div>
<?php endif; ?>

I have searched in google a lot to find the solution. But did not find. Please help someone.

Upvotes: 0

Views: 392

Answers (1)

Ali MasudianPour
Ali MasudianPour

Reputation: 14459

As a suggestion, do like below in your view:

<script type="text/javascript">
    function captchaInit(){
            $("#yw0_button").click();
    }
</script>

Then, In your body onload call that function.

<body onload="captchaInit()"> 

It causes to reload the Captcha in body load. In similar cases it works to refresh the captcha for every page refresh.

Upvotes: 1

Related Questions