I’m having a hard time with the captcha in yii2. I am working on localhost and I would simply like to have a captcha in my contact form, but the captcha images will not be generated no matter what I do.
I have Imagick installed and basically kept the advanced installation just like it was. Also every required package was installed. After some debugging I found out that it seems like the first if-statement in public function run()
in the captcha file located in vendor/yiisoft/yii2/captcha
evaluates to false, even though it should be true.
So when I change this
if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) !== null) {
// AJAX request for regenerating code
$code = $this->getVerifyCode(true);
Yii::$app->response->format = Response::FORMAT_JSON;
return [
'hash1' => $this->generateValidationHash($code),
'hash2' => $this->generateValidationHash(strtolower($code)),
// we add a random 'v' parameter so that FireFox can refresh the image
// when src attribute of image tag is changed
'url' => Url::to([$this->id, 'v' => uniqid('', true)]),
];
}
to this:
if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) == null) {
// AJAX request for regenerating code
$code = $this->getVerifyCode(true);
Yii::$app->response->format = Response::FORMAT_JSON;
return [
'hash1' => $this->generateValidationHash($code),
'hash2' => $this->generateValidationHash(strtolower($code)),
// we add a random 'v' parameter so that FireFox can refresh the image
// when src attribute of image tag is changed
'url' => Url::to([$this->id, 'v' => uniqid('', true)]),
];
}
I don’t understand why the following line is evaluated to null:
if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) == null) {
it will actually create a JSON object and pass it to the frontend, but with an invalid image id, as it was not generated, just passed the link. I’m actually concerned that it does not find Imagick or something, even though there’s no error in requirements.php
.
contact.php
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;
$this->title = 'Contact';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-contact">
<h1><?= Html::encode($this->title) ?></h1>
<p>
If you have business inquiries or other questions, please fill out the following form to contact us. Thank you.
</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>
<?= $form->field($model, 'name')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'subject') ?>
<?= $form->field($model, 'body')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'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>
</div>
ContactForm.php
<?php
namespace frontend\models;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
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'],
];
}
public function attributeLabels()
{
return [
'verifyCode' => 'Verification Code',
];
}
public function sendEmail($email)
{
return Yii::$app->mailer->compose()
->setTo($email)
->setFrom([Yii::$app->params['senderEmail'] => Yii::$app->params['senderName']])
->setReplyTo([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
}
}
This is the error message when trying to access the image link: Translated: The graphic cannot be displayed because it contains errors.
See my original post from stack overflow: https://stackoverflow.com/questions/62405103/yii2-captcha-image-does-not-exist
Has anyone encountered something similar? Any help highly appreciated!