2

我正在使用 Yii 1.1 和一些bootstrap.widgets. 我正在尝试使用 Yii 在我的注册表单中CCaptcha使用CCaptchaAction.

目前我有一个UserController处理验证码图像的渲染并设置它accessRules以便可以使用验证码。这RegisterForm是一个模型,我在其中设置了一个verifyCode针对 CAPTCHA 类型进行验证的规则,并且它可能不是空的。最后Register是我的观点,它只是使用小部件提供的形式呈现表单TbActiveForm。根据YiiBooster 的文档TbActiveForm,它是 的扩展版本CActiveForm,所以ajaxValidation应该禁用,我已经这样做了。

我正在努力解决验证总是失败的事实:verifyCode-field 的输入是正确的,所以我认为验证失败的原因是我没有将它与表单中显示的 CAPTCHA 进行比较。但是,我不确定这一点,我不知道如何测试它。这是我得到的错误:

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

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

用户控制器.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));
    }

}

注册表格.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',
        );
    }
}

注册.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>
4

0 回答 0