Yii2模块中的无效验证码操作ID [英] Yii2 Invalid CAPTCHA action ID in module

查看:51
本文介绍了Yii2模块中的无效验证码操作ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义contactus模块中收到了无效的验证码操作ID异常.我设法显示了验证码,但模型验证规则引发了无效的操作ID异常.下面是我的代码:

I am getting the Invalid CAPTCHA action ID Exception in my custom contactus module. I managed to display the captcha but models validation rule throws the invalid action ID exception. Below is my code:

contactus/controllers/DefaultController.php

contactus/controllers/DefaultController.php

class DefaultController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => \yii\filters\AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['captcha','index'],
                        'allow' => true,
                    ],
                ]
            ]
        ];
    }

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

    public function actionIndex()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(setting::ADMIN_EMAIL_ADDRESS)) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        } else {
            return $this->render('index', [
                'model' => $model,
            ]);
        }
    }
}

contactus/models/ContactForm.php

contactus/models/ContactForm.php

public function rules()
{
    return [
        // name, email, subject and body are required
        [['name', 'email', 'subject', 'body','verifyCode'], 'required'],
        // email has to be a valid email address
        ['email', 'email'],
        // verifyCode needs to be entered correctly
        ['verifyCode', 'captcha','captchaAction'=>'default/captcha'],
    ];
}

contactus/views/default/index.php

contactus/views/default/index.php

 <?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' => 'default/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(); ?>

我收到以下错误:

Exception (Invalid Configuration) 'yii\base\InvalidConfigException' with message 'Invalid CAPTCHA action ID: default/captcha'in E:\wamp\www\yii-application\vendor\yiisoft\yii2\captcha\CaptchaValidator.php:81

我想念什么吗?

推荐答案

您应修改验证规则:

['verifyCode', 'captcha','captchaAction'=>'/contactus/default/captcha'],

这篇关于Yii2模块中的无效验证码操作ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆