I'm trying to add captcha to my login page on my yii2 application.
I have tried some tutorial, the problem is captcha is always correct like no validation.
I have tried this:
- https://www.yiiframework.com/doc/api/2.0/yii-captcha-captcha
- https://mumunotesss.blogspot.com/2015/06/implementation-captcha-on-yii2.html
- http://yii2-user.readthedocs.io/en/latest/howto/adding-captcha.html
- How to add captcha in Yii-2 application?
My code is:
LoginForm
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
public $captcha; // add this varible to your model class.
/**
* @return array the validation rules.
*/
public function rules() {
return [
// username and password are both required
[['username', 'password','captcha'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
['captcha', 'captcha','captchaAction'=>'/site/captcha'], // add this code to your rules.
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
SiteController
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
view login.php
<div class="form-goup">
<?= $form->field($model, 'captcha')->widget(yii\captcha\Captcha::className(), '
['template' => '<div class="row"><div class="col-lg-3" style="margin-right:25px;">{image}</div><div class="col-lg-6">{input}</div></div>',
]); ?>
</div>