我试图将验证码与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; ?>
我已将此数组添加到comment controller中的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,
),
);
}
在评论模型中,我添加了一条新规则:
array('verifyCode', 'captcha', 'on' => 'insert', 'allowEmpty'=>!Yii::app()->user->isGuest)
和新的公共成员:
public $verifyCode;
因为评论表单是从博客actionView()中显示的,所以我认为它会产生问题。验证码显示但从不验证。有什么想法吗?
答案 0 :(得分:0)
您还需要将captchaAction添加到Comment模型验证规则中,如下所示:
array('verifyCode', 'captcha', 'on' => 'insert', 'allowEmpty'=>!Yii::app()->user->isGuest, 'captchaAction'=>'comment/captcha')