加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Yii2模块中的CAPTCHA操作ID无效

发布时间:2020-12-13 18:08:27 所属栏目:PHP教程 来源:网络整理
导读:我在自定义联系人模块中收到了无效的CAPTCHA操作ID异常.我设法显示验证码,但模型验证规则会抛出无效的操作ID异常.以下是我的代码: 联系我们/控制器/ DefaultController.php class DefaultController extends Controller{ public function behaviors() { ret
我在自定义联系人模块中收到了无效的CAPTCHA操作ID异常.我设法显示验证码,但模型验证规则会抛出无效的操作ID异常.以下是我的代码:

联系我们/控制器/ DefaultController.php

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

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yiiwebErrorAction','captcha' => [
                'class' => 'yiicaptchaCaptchaAction','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,]);
        }
    }
}

联系我们/模型/ 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'],];
}

联系我们/视图/默认/ 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) 'yiibaseInvalidConfigException' with message 'Invalid CAPTCHA action ID: default/captcha'in E:wampwwwyii-applicationvendoryiisoftyii2captchaCaptchaValidator.php:81

我错过了什么吗?

您应该修改验证规则:
['verifyCode','captchaAction'=>'/contactus/default/captcha'],

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读