Log in

Sign in with your email address and password:

No account? Sign up here.
Forgot your password? Reset it here.
Didn't receive account confirmation email? Request a new one.


A Refreshable Captcha In Yii2

By default, Yii2’s captcha doesn’t come with a ‘refresh’ link/button which lets you refresh the image. This can be frustrating as often the image is quite unreadable (even for humans!). Below is an implementation of CaptchaRefreshable which adds a refresh link to the standard Yii2 captcha. This widget is included in the Foundationize Yii2 with Foundation 6 configuration. A Refreshable Captcha in Yii2

CaptchaRefreshable

Using the refreshable captcha

To use this refreshable captcha in your view (no longer do you need to include the template as an option when creating the verifyCode field):

What I found happens when refreshing the captcha is, on form submit, the model validation will fail because the ajax validation (seems you cannot disable it for captcha widget) will cause a new captcha to be generated and subsequent validation fails.

Avoiding captcha refresh failure with CaptchaRefreshableAction

Add the following class to your frontend/models folder:

In your SiteController, update your actions() method captcha entry to point to new class:

Your refreshed captcha should now work correctly.

Thoughts? Please don’t hesitate to let us know in the comments below, thanks!


Submit a Comment