2013-01-04 34 views
1

我已經在這裏應用所有其他解決方案關於破壞yii驗證碼,但無濟於事。所以我添加了我自己的問題。Yii captcha不能與博客合作

我已經通過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'); ?> 
      <?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; ?> 

而且在CommentController,accessRules()變爲:

public function accessRules() 
{ 
     return array(
       array('allow', // allow all users to perform 'index' and 'view' actions 
         'actions'=>array('index','view', 'captcha'), 
         'users'=>array('*'), 
       ), 
       array('allow', // allow authenticated user to perform 'create' and 'update' actions 
         'actions'=>array('create','update'), 
         'users'=>array('@'), 
       ), 
       array('allow', // allow admin user to perform 'admin' and 'delete' actions 
         'actions'=>array('admin','delete'), 
         'users'=>array('admin'), 
       ), 
       array('deny', // deny all users 
         '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; 

聯繫表上的驗證碼工作正常。但是在評論表單中,圖像被破壞,刷新它的鏈接不起作用。有任何想法嗎?

回答

0

你打開過你的螢火蟲或網頁檢查器嗎?它對你的驗證碼請求有什麼迴應?

+0

感謝您的回覆。我使用了firbug,當我刷新聯繫人表單上的驗證碼時,其中有2個GET請求用於captcha?refresh = 1&_ = 1357396271704,然後驗證碼?v = 50e8392fcc7a9。但是,在博客下,請求captcha?refresh = 1&_ = 1357396217245,但響應是空的,然後向站點/登錄請求... – user1716672

+2

嘗試調用您的驗證碼窗口小部件:$ this-> widget ('CCaptcha',數組('captchaAction'=>'comment/captcha'))。文檔說,驗證碼請求是對當前控制器進行的,看起來好像您的評論表單顯示在另一個控制器的操作中。 –

+0

工作,謝謝你! – user1716672

1

我將此代碼添加到博客演示中,它看起來像驗證碼的請求將發送到PostController而不是CommentController。如果您將驗證碼操作添加到PostController,它應該可以工作。

+0

是的,你是對的,那是問題所在。謝謝你 – user1716672