Yii2 验证码总是不正确

Yii2 captcha always incorrect

当我在规则中添加 'on' => 'create' 时,我的验证码总是不正确 关闭那个工作好为什么? 我试着让它传给其他控制器 support/captcha 但仍然不正确

我的视图

 <?= Captcha::widget([
                        'id' => 'captcha',
                        'model' => $model,
                        'attribute' => 'verifyCode',
                        'options' => ['class' => 'form-control',
                            'data-v-rule' => '',
                            'captchaAction' => 'support/captcha',
                            'data-v-msg' => $i18n->t('please.input.verify.code'),
                            'placeholder' => $i18n->t('please.input.verify.code', '', false)]
                    ]); ?>

Myrule

...

        [['verifyCode'], 'captcha', 'message' => $i18n->t('error.verifyCode'),'captchaAction' => 'support/captcha' , 'on' => 'create'],
...

我看到了

$_SESSION = [
    '__flash' => [],
    '__captcha/site/captcha' => 'nnbioo',
    '__captcha/site/captchacount' => 1,
    '__captcha/support/captcha' => 'cacijq',
    '__captcha/support/captchacount' => 1,
];

我的控制器

public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => CaptchaAction::className(),
            ],
        ];
    }

$customerServiceModel->load($post) 不起作用,因为在这种情况下属性 verifyCode 不是 safe。您的规则是用 'on' => 'create' 指定的 - 所以这意味着它在场景 create 上保存。您没有将此场景分配给模型,因此有 2 个解决方案:

  1. 从规则中删除 'on' => 'create'
  2. 在使用 load() 之前,通过 $customerServiceModel->scenario = 'create';create 场景分配给模型。