如何在Yii2中重新使用该字段重新连接谷歌扩展程序himiklab

时间:2017-01-06 11:38:39

标签: validation yii2 recaptcha yii2-extension

我正在使用扩展程序himiklab进行yii2 recaptcha,这与谷歌类似。我想在我的规则中将此字段设置为必填字段。当我将其设置如下时,即使我没有点击该复选框,也无法验证。

  [['reCaptcha'], 'required'],
      ['reCaptcha', \himiklab\yii2\recaptcha\ReCaptchaValidator::className(), 'secret' => '***','skipOnEmpty' => false],

视图

<?= $form->field($model, 'reCaptcha')->widget(
    \himiklab\yii2\recaptcha\ReCaptcha::className(),
    ['siteKey' => '6LeY1BAUAAAAALThRhBQ-sJaXbP0Z5i9XFuaz_VW']
)->label(false); ?>

动作

  public function actionSignup()
    {

        $browser = new Browser;
        if( $browser->getBrowser() == Browser::BROWSER_IE && $browser->getVersion() < 11 ) 
            {   
                return $this->render('browser');                                                                                                                                   
        }

        $company = new Company(); 
        $model = new SimUser(['scenario' => SimUser::SCENARIO_REGISTER]);

        if ($model->load(Yii::$app->request->post())&& $model->validate() && $company->load(Yii::$app->request->post())&& $company->validate()) {
           $model->scenario = SimUser::SCENARIO_REGISTER;
           $model->setPassword($model->user_password_hash);
          // $model->setCaptcha($model->captcha);
            $model->generateAuthKey(); 
            $token = Yii::$app->security->generateRandomString();
            $model->user_access_token = $token;
            $model->user_verify = 1;
           // $company->save();
            $model->company_id = 3; 
          //  $model->save();
            $model->user_id = 44;
            var_dump($model->validate());exit();

            if ($model->validate()){
//                $auth = Yii::$app->authManager;
//                $authorRole = $auth->getRole('Company Admin');
//                $auth->assign($authorRole, $model->user_id);
                $path = 'C:/wamp/www/test.qsims.com/web/gentelella-1.2.0/production/images/DCMLogo.png';
                Yii::$app->mailer->compose('@app/mail/layouts/verify',['model' => $model, 'path' => $path,'token' => $model->user_access_token])
                       ->setTo($model->user_email)
                       ->setFrom('test.qsims@gmail.com')
                       ->setSubject('Welcome to Qsims'.$model->user_fname." ".$model->user_lname.'. Verify your account to continue')
                       ->setTextBody('Verify Account')
                       ->send(); 
            }

          //  \Yii::$app->user->login($model);
              return $this->redirect(['site/verify-new']);
        }

        return $this->render('signup', [
            'model' => $model,
            'company' => $company,

        ]);
    }

我哪里错了?

1 个答案:

答案 0 :(得分:0)

Add this to the model
Public $reCaptcha;
add this to rules
 ['reCaptcha', 'reCaptchaValidator']

call the custom validation
 public function reCaptchaValidator($attribute)
    {
        $validator = new ReCaptchaValidator;
        if (!$validator->validate($this->reCaptcha, $error)) {
            $this->addError($attribute, $error);
        }

    }