2013-01-05 77 views
0

我試圖將驗證碼與Yii博客(http://www.yiiframework.com/doc/blog/)集成,以便用戶必須填寫評論表單中的驗證碼。 在註釋形式視圖我已經添加yii博客驗證碼從不驗證

<?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重寫操作():

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

你還需要將captchaAction添加到您的評論模型驗證規則,如:

array('verifyCode', 'captcha', 'on' => 'insert', 'allowEmpty'=>!Yii::app()->user->isGuest, 'captchaAction'=>'comment/captcha') 
+0

好極了,它的工作。謝謝你! – user1716672