Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 captcha refresh button

I am using the default captcha implementation of the yii2 advanced framework. I have a problem: I want to add a refresh button to my captcha but I do not know how can I do it

like image 691
Mohsen Jalalian Avatar asked Jul 16 '26 03:07

Mohsen Jalalian


1 Answers

Add captcha action to your controller, add captcha to your form, add field and validate rules for captcha to your model. Read more here.

And add button in view for update captcha image, like this:

<?php echo $form->field($model, 'captcha')->widget(Captcha::className(), [
    'imageOptions' => [
        'id' => 'my-captcha-image'
    ]
]); ?>

<?php echo Html::button('Refresh captcha', ['id' => 'refresh-captcha']);?>
<?php $this->registerJs("
    $('#refresh-captcha').on('click', function(e){
        e.preventDefault();

        $('#my-captcha-image').yiiCaptcha('refresh');
    })
"); ?>
like image 163
Vitaly Avatar answered Jul 17 '26 16:07

Vitaly