Capcha总是显示不正确

Pra*_*til 6 captcha yii2

我在我的一个表格中使用过capcha.它总是给验证码错误.

以下是我的代码:

SchoolsController.php

public function actions() {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fontFile' => '@yii/captcha/SpicyRice.ttf',
                'padding' => '0',
            ]
        ];
    }
Run Code Online (Sandbox Code Playgroud)

模型:

['verifyCode', 'captcha', 'captchaAction' => 'schools/captcha'],
Run Code Online (Sandbox Code Playgroud)

视图:

echo $form->field($modelSchoolRequestEarlyAccess, 'verifyCode')->widget(Captcha::className(), [
                        'options' => [
                            'placeholder' => 'Enter characters in the image',
                            'autocomplete' => 'off',
                            'maxlength' => 20 
                        ],
                        'captchaAction' => 'schools/captcha',
                        'template' => "<div class='field'><span><strong>*</strong>".Yii::t('frontend/quicksignup','VerifyCodeLabel').":</span></span>\n<div>{image}{input}<i class='refresh-code-icn' id='get-new-code'></i></div></div>"
                        ])->label(false); 
Run Code Online (Sandbox Code Playgroud)

我已经schools/captcha在模型和视图中指定了验证码操作.但它总是显示验证不正确.

我究竟做错了什么??

小智 0

你添加规则了吗?

   public function rules()
{
    $rules = parent::rules();
    $rules[] = ['captcha', 'required'];
    $rules[] = ['captcha', 'captcha'];
    return $rules;
}
Run Code Online (Sandbox Code Playgroud)