0

我试图将验证码与 Yii 博客 (http://www.yiiframework.com/doc/blog/) 集成,以便用户必须在评论表单中填写验证码。在我添加的评论表单视图中

    <?php if(CCaptcha::checkRequirements()): ?>
    <div class="row">
            <?php echo $form->labelEx($model,'verifyCode'); ?>
            <div>
            <?php $this->widget('CCaptcha', array('captchaAction'=>'comment/captcha')); ?>
            <?php echo $form->textField($model,'verifyCode'); ?>
            </div>
            <div class="hint">Please enter the letters as they are shown in the image above.
            <br/>Letters are not case-sensitive.</div>
            <?php echo $form->error($model,'verifyCode'); ?>
    </div>
    <?php endif; ?>

我已将此数组添加到评论控制器中的 accessRules() 中:

            array('allow',  // allow all users to perform 'index' and 'view' actions
                    'actions'=>array('index','view', 'captcha'),
                    'users'=>array('*'),
            ),

我在 CommentController 中覆盖了 actions():

public function actions()
{
    return array(
        // captcha action renders the CAPTCHA image displayed on the contact page
        'captcha'=>array(
            'class'=>'CCaptchaAction',
            'backColor'=>0xD99D25,
        ),
    );
}  

在 Comment 模型中,我添加了一条新规则:

array('verifyCode', 'captcha', 'on' => 'insert', 'allowEmpty'=>!Yii::app()->user->isGuest)

和新的公众成员:

public $verifyCode;

因为评论表单是从博客 actionView() 中显示出来的,所以我认为它会产生问题。验证码显示但从不验证。有任何想法吗?

4

1 回答 1

0

您还需要将 captchaAction 添加到您的 Comment 模型验证规则中,如下所示:

array('verifyCode', 'captcha', 'on' => 'insert', 'allowEmpty'=>!Yii::app()->user->isGuest, 'captchaAction'=>'comment/captcha')
于 2013-01-06T11:53:53.120 回答