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

问题描述

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

contactus/controllers/DefaultController.

class DefaultController 扩展控制器{公共功能行为(){返回 ['访问' =>['类' =>yiifiltersAccessControl::className(),'规则' =>[['动作' =>['验证码','索引'],'允许' =>真的,],]]];}公共功能动作(){返回 ['错误' =>['类' =>'yiiwebErrorAction',],'验证码' =>['类' =>'yii验证码验证码操作','fixedVerifyCode' =>YII_ENV_TEST ?'测试':空,],];}公共函数 actionIndex(){$model = new ContactForm();if ($model->load(Yii::$app->request->post()) && $model->contact(setting::ADMIN_EMAIL_ADDRESS)) {Yii::$app->session->setFlash('contactFormSubmitted');返回 $this->refresh();} 别的 {返回 $this->render('index', ['模型' =>$模型,]);}}}

contactus/models/ContactForm.

公共函数规则(){返回 [//姓名、电子邮件、主题和正文是必需的[['name', 'email', 'subject', 'body','verifyCode'], 'required'],//电子邮件必须是有效的电子邮件地址['电子邮件','电子邮件'],//需要正确输入验证码['verifyCode', 'captcha','captchaAction'='default/captcha'],];}

contactus/views/default/index.

 <?= $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(), ['验证码操作' =>'默认/验证码','模板' =>'<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']) ?>

<?

我收到以下错误:

Exception (Invalid Configuration) 'yiiaseInvalidConfigException' with message 'Invalid CAPTCHA action ID: default/captcha'in E:wampwwwyii-applicationvendoryiisoftyii2captcha验证码验证器.

我错过了什么吗?

你应该修改你的验证规则:

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

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.

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,
            ]);
        }
    }
}

contactus/models/ContactForm.

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.

 <?

I get the below error:

Exception (Invalid Configuration) 'yiiaseInvalidConfigException' with message 'Invalid CAPTCHA action ID: default/captcha'in E:wampwwwyii-applicationvendoryiisoftyii2captchaCaptchaValidator.

Am I missing something?

You should modify your validation rule :

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

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