Yii2验证码刷新按钮

时间:2016-06-25 11:14:53

标签: php yii yii2 yii2-advanced-app

我正在使用yii2高级框架的默认验证码实现。我有一个问题:我想在我的验证码上添加一个刷新按钮,但我不知道怎么办呢

1 个答案:

答案 0 :(得分:9)

将captcha操作添加到您的控制器,将验证码添加到您的表单,添加字段并验证captcha到您的模型的规则。阅读更多here

在视图中添加按钮以获取更新验证码图像,如下所示:

<?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');
    })
"); ?>