【问题标题】:ReCaptcha throws out configuartion error in Yii2ReCaptcha 在 Yii2 中抛出配置错误
【发布时间】:2017-06-30 10:18:39
【问题描述】:

以下代码抛出如下错误: “无效配置 – yii\base\InvalidConfigException 必须指定“名称”或“模型”和“属性”属性。” 我完全不知道如何解决这个问题。任何可能的帮助? 这是我的配置文件:

return [
    'aliases' =>
    [
        '@uploadedfilesdir' => '@app/mails',
        '@uploading' => '@app/uploadedfiles'
    ],
    'components' => [
        'reCaptcha' => [
            'class' => 'himiklab\yii2\recaptcha\ReCaptcha',
            'siteKey' => 'siteKey',
            'secret' => 'secret key'
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
        ],
        'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => true,
            'enableStrictParsing' => true,
            'rules' => [
                '/' => 'site/index',
                'reset' => 'site/request-password-reset',
                'login' => 'site/login',
                'contact' => 'site/contact',
                'logout' => 'site/logout',
                'signup' => 'site/signup',
                'formular' => 'site/script',
                'praktikum' => 'bewerbungen/index',
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                'country' => 'country/index'
            ],
        ],
        'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host=localhost;dbname=yii2_widget',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8',
        ],
    ],
];
?>

这是我的模型:

<?php

namespace frontend\models;

use Yii;
use yii\base\Model;

/**
 * ContactForm is the model behind the contact form.
 */
class ContactForm extends Model {

    public $name;
    public $email;
    public $subject;
    public $body;
    public $reCaptcha;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['name', 'email', 'subject', 'body'], 'required'],
            ['email', 'email'],
            ['reCaptcha', \himiklab\yii2\recaptcha\ReCaptchaValidator::className(), 'secret' => '6LeTXQgUAAAAALExcpzgCxWdnWjJcPDoMfK3oKGi']
        ];
    }

    public function attributeLabels() {
        return['reCaptcha' => '',];
    }

    public function sendEmail($email) {
        return Yii::$app->mailer->compose()
                        ->setTo($email)
                        ->setFrom([$this->email => $this->name])
                        ->setSubject($this->subject)
                        ->setTextBody($this->body)
                        ->send();
    }

}
这是我的公式:

$this->title = 'Contact';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-contact">
    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        If you have business inquiries or other questions, please fill out the following form to contact us. Thank you.
    </p>

    <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'contact-form']); ?>

                <?= $form->field($model, 'name')->textInput(['autofocus' => true]) ?>

                <?= $form->field($model, 'email') ?>

                <?= $form->field($model, 'subject') ?>

                <?= $form->field($model, 'body')->textarea(['rows' => 6]) ?>

             <?= $form->field($model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className()) ?>

                <div class="form-group">
                    <?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'contact-button']) ?>
                </div>

            <?php ActiveForm::end(); ?>
        </div>
    </div>

</div>

【问题讨论】:

    标签: yii2


    【解决方案1】:

    根据它的文档,您应该在组件数组中添加'name' =&gt; 'reCaptcha'

    'components' => [
        'reCaptcha' => [
            'name' => 'reCaptcha',
            'class' => 'himiklab\yii2\recaptcha\ReCaptcha',
            'siteKey' => 'your siteKey',
            'secret' => 'your secret key',
        ],
    

    在模型中你应该声明它的模型property

    public $reCaptcha; //<-- Model
    
    public function rules()
    {
      return [
          // ...
          [['reCaptcha'], \himiklab\yii2\recaptcha\ReCaptchaValidator::className(), 'secret' => 'your secret key', 'uncheckedMessage' => 'Please confirm that you are not a bot.']
      ];
    }
    

    并在您的模板中使用'reCaptcha' 模型属性:

         <?= $form->field($model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className()) ?>
    

    【讨论】:

    • 我发布了您的所有建议!但它会抛出错误!
    • As error stats `yii\base\InvalidConfigException 必须指定“name”或“model”和“attribute”属性。`您没有在 reCaptcha 的配置组件数组中提供组件 name你应该添加'name' =&gt; 'reCaptcha',
    • 答案左上角有右勾按钮。
    猜你喜欢
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 2016-10-31
    相关资源
    最近更新 更多