ReCaptcha在Yii2中抛出了configuartion错误

时间:2017-06-30 10:18:39

标签: yii2

以下代码抛出错误,如下所示: “无效的配置 - 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>

1 个答案:

答案 0 :(得分:1)

根据它的文档,您应该在组件数组中添加'name' => '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()) ?>