Adding captcha to forms is pretty easy and can be done in three steps:
- Add new field and validation rule in the model
- Show captcha in view
- Add captcha action in controller
In this guide I would like to show you how to add captcha field in the registration form but you can add captcha to any form following this steps.
NOTE: Captcha will not work with ajax validation enabled (see). You should not enable it in active form configuration array.
First of all you need to override Registration form as described in special guide. After this done you have to add public property named captcha and validation rules.
<?php
namespace app\models;
class RegistrationForm extends \dektrium\user\models\RegistrationForm
{
/**
* @var string
*/
public $captcha;
/**
* @inheritdoc
*/
public function rules()
{
$rules = parent::rules();
$rules[] = ['captcha', 'required'];
$rules[] = ['captcha', 'captcha'];
return $rules;
}
}
Before doing this step you have to configure view application component as described in guide. After this done you have
to create new file named register.php
in @app/views/user/registration
. Now you have to add widget to registration
form, just copy and paste following code into newly created view file.
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\captcha\Captcha;
/**
* @var yii\web\View $this
* @var yii\widgets\ActiveForm $form
* @var app\models\RegistrationForm $model
*/
$this->title = Yii::t('user', 'Sign up');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><?= Html::encode($this->title) ?></h3>
</div>
<div class="panel-body">
<?php $form = ActiveForm::begin([
'id' => 'registration-form',
]); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'captcha')->widget(Captcha::className(), [
'captchaAction' => ['/site/captcha']
]) ?>
<?= Html::submitButton(Yii::t('user', 'Sign up'), ['class' => 'btn btn-success btn-block']) ?>
<?php ActiveForm::end(); ?>
</div>
</div>
<p class="text-center">
<?= Html::a(Yii::t('user', 'Already registered? Sign in!'), ['/user/security/login']) ?>
</p>
</div>
</div>
In order to make captcha work you have to add captcha action to app\controllers\SiteController
Maybe it is already
added because standard Yii2 application template adds it automatically.
<?php
namespace app\controllers;
class SiteController extends \yii\web\Controller
{
...
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
],
];
}
...
}