J
J
Julia Kovalenko2017-03-16 16:30:31
Yii
Julia Kovalenko, 2017-03-16 16:30:31

How to use reCaptcha in yii2?

I am using the library https://github.com/himiklab/yii2-recaptcha-widget
In the model:

class MyClass extends \yii\db\ActiveRecord
{
   public $reCaptcha;

    public function rules()
    {
        return [
            [['reCaptcha'], \himiklab\yii2\recaptcha\ReCaptchaValidator::className(),
            	'secret' => 'secretkey',
            	'uncheckedMessage' => 'Пожалуйста, подтвердите, что вы не робот.']
        ];
    }

In View:
<?= $form->field($model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className()) ?>

Everything works great!
But I need to throw a recaptcha in the form not always, but only in some cases.
Something like:
<?php echo $captcha ?
    $form->field($model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className()) : " " ?>

How to implement it? If you do as above, just do not display the recaptcha in the view, then the model will not be saved. There will be an error:
["reCaptcha"]=>
    array(1) {
      [0]=>
      string(55) "Неправильный проверочный код."
    }

How it is better to implement it?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
M
Maxim Fedorov, 2017-03-16
@kovalenko_jul_s

use scripts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question