Yii的CCaptcha无法验证

时间:2017-05-04 20:24:28

标签: php yii captcha yii-booster

我正在使用Yii 1.1和一些bootstrap.widgets。我正在尝试使用Yii的CCaptchaCCaptchaAction在我的注册表单中实施CAPTCHA。

目前我有UserController处理CAPTCHA图像的渲染并设置accessRules以便可以使用CAPTCHA。 RegisterForm是一个模型,我在其中设置了一个规则,verifyCode针对CAPTCHA类型进行了验证,并且它可能不为空。最后Register是我的观点,它只是使用小部件提供的TbActiveForm呈现表单。根据我所做的YiiBooster's documentation TbActiveForm is an extended version of CActiveForm, so ajaxValidation should be disabled

我正在努力解决验证始终失败的问题:verifyCode - 字段的输入是正确的,因此我认为验证失败的原因是我没有将它与显示的CAPTCHA进行比较在形式。但是,我不确定这一点,我不知道如何测试它。这是我得到的错误:

  

array(1){[" verifyCode"] => array(1){[0] => string(35)" The   验证码不正确。" }}

this问题的答案对我没有帮助。

UserController.php

class UserController extends Controller
{

    /**
     * Declares external action controller
     */
    public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the register page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF
            )
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array(
                'allow',
                'actions' => array(
                    'register',
                    'captcha'
                )
            )
        );
    }

    /**
     * Register new user
     */
    public function actionRegister()
    {
        $model = new RegisterForm(); // Create new register-form
        $request = request();
        if ($request->isPostRequest) {
            $model->attributes = $request->getPost('RegisterForm');
            if (!$model->validate()) {
              var_dump($model->getErrors());
            }
            if ($model->validate()) {
              var_dump('Got here, so it works!');
            }
        }
        $this->render('register', array('model' => $model));
    }

}

RegisterForm.php

class RegisterForm extends CFormModel
{
    public $verifyCode;

    public function rules()
    {
        return array(
            array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements())
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels()
    {
        return array(
            'verifyCode' => 'Verification Code',
        );
    }
}

Register.php

<div>
  <?php
    $form = $this->beginWidget(
        'bootstrap.widgets.TbActiveForm',
        array(
            'id' => 'register-form',
            'layout' => TbHtml::FORM_LAYOUT_HORIZONTAL
        )
    );
  ?>
  <fieldset>
      <?php
        if(CCaptcha::checkRequirements()):
          echo $form->labelEx($model, 'verifyCode');
          $this->widget('CCaptcha');
          echo $form->textField($model, 'verifyCode', array(), false, false);
          echo $form->error($model, 'verifyCode', array(), false, false);
        endif;
      ?>
  </fieldset>
  <?php
    echo TbHtml::submitButton(
        t('user', 'Register'),
        array(
          'id' => 'submit-button',
          'color' => TbHtml::BUTTON_COLOR_PRIMARY,
          'size' => TbHtml::BUTTON_SIZE_LARGE,
          'block' => true
        )
    );
    $this->endWidget();
  ?>
</div>

0 个答案:

没有答案