Captcha not refreshing

I have this code

Controller:




public function actions()

    {

        return [

            'captcha' => [

                'class' => 'yii\captcha\CaptchaAction',

                'transparent' => true,

                

            ],

        ];

    }



Model:




public function rules()

    {

        return [

            [['captcha'], 'required'],

            ['captcha', 'captcha'],

        ];

    }



View:




<div class="col-sm-12">

    <?= $form->field($model, 'captcha')->widget(Captcha::className(), [

                        'template' => '<div class="row"><div class="col-lg-3">{image}<i class="fa fa-refresh fa-lg"></i></div><div class="col-lg-6">{input}</div></div>',

                    ]) ?>

</div>



It works fine except it doesn’t refresh after sending the post. I’ve tried with more browsers with no luck. It changes only when a new session is created.

Any idea about how to solve it?

From what I have seen, it will give you the same one over and over again until you have typed it in correctly. Then when you go to the page again, will you see a new one.

No, it doesn’t change. The user submits the form and the captcha doesn’ change. With a simple PHP script anyone could send the form thousands of times just knowing the captcha.

Does it validate it?

If you type it wrong does it tell you it’s wrong?

Okay, I think you are missing this part.

In your model class and in your specific action method for the form, you must include the following:




        if ($this->validate()) {

            return true;

        } else {

            return false;

        }



If $this->validate() is true, it will generate a new captcha for you.

That’s it. I wasn’t using validation because it’s a contact form and there’s nothing to save in the DB. I knew something about the need of a validation but I thought it was the captcha validation itself, not the model one.

It’s actually $model->validate() because $this is a Controller instance.

Thanks.