I’m stuck on implementing a captcha outside of site controller. I moved contact to pages controller and view. I researched this issue and did the following:
PagesController:
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['captcha'],
'rules' => [
[
'actions' => ['captcha'],
'allow' => true,
'roles' => ['?', '@'],
],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
ContactForm model:
public $verifyCode;
/**
* @inheritdoc
*/
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha', 'captchaAction' => 'pages/captcha'],
];
}
View:
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'subject') ?>
<?= $form->field($model, 'body')->textArea(['rows' => 6]) ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'captchaAction' => 'pages/captcha',
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>',
]) ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
It’s not returning an error message. But the captcha image does not change and always returns a validation error saying the verification code is incorrect, even though it is.
I wasn’t clear if the behaviors method was needed, so I tried it with and without and it still doesn’t work.
I referenced:
https://github.com/y...yii2/issues/693
https://github.com/y...ii2/issues/1889
At this point, I’m out of ideas. Can anyone see what I’m doing wrong?
SOLVED: last step needed, clear cache…