Adding and customizing CaptchaWidget

Nowadays, on the Internet, if you leave a form without spam protection, you will get a ton of spam data entered in a short time. Yii includes a Captcha component that makes adding such protection a breeze. The only problem is that there is no systematic guide on how to use it.

In the following example, we will add Captcha protection to a simple form.

Getting ready

  1. Create a new application by using the Composer package manager, as described in the official guide at http://www.yiiframework.com/doc-2.0/guide-start-installation.html.
  2. Create a form model, @app/models/EmailForm.php, as follows:
    <?php namespace app\models; use yii\base\Model; class EmailForm extends Model { public $email; public function rules() { return ...

Get Yii2 Application Development Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.