2015-02-05 35 views
0

嗨我試圖盡我所能在Yii驗證碼實現,但我卡住了。有些奇怪的東西。Yii captcha奇怪的消息錯誤行爲

1.即使我寫得正確,消息錯誤也會顯示出來。我必須按「獲取新代碼」,然後才能正常工作。查看圖片: enter image description here

2.此消息的錯誤是由於某種原因,我想關鍵事件產生的。我只想在按下一個提交按鈕的情況下生成消息錯誤,該提交按鈕會執行ajax請求。

這是我模式

public $verifyCode; 
.... 
public function rules() { 
     // NOTE: you should only define rules for those attributes that 
     // will receive user inputs. 

     if ($this->scenario == "insert") { 
      return array(
       array('requisition_id, sync', 'numerical', 'integerOnly' => true), 
       array('lastname, firstname, email, dob, phone, cv_path, experienceMonths, experienceYears, competencies, token', 'required', 'message' => "Câmpul este obligatoriu"), 
       array('email', 'email', 'message' => "Emailul este invalid!"), 
       array('dob', 'validateDob'), 
       array('dayOfBirth, monthOfBirth, yearOfBirth', 'safe'), 
       array('taleo_id, sync', 'required', 'on' => 'taleoUpdate'), 
       array('verifyCode', 'captcha', 'captchaAction'=>'site/captcha') 
       // The following rule is used by search(). 
       // Please remove those attributes that should not be searched. 
      ); 
     } else if ($this->scenario == 'taleoUpdate') { 
      return array(
       array('taleo_id, sync', 'required'), 
       // The following rule is used by search(). 
       // Please remove those attributes that should not be searched. 
      ); 
     } 
     else if($this->scenario == 'notjobapply'){ 
      return array(
       array('lastname, firstname, email, phone, cv_path, requisition_id', 'required', 'message'=>'Câmpul este obligatoriu'), 
       array('email', 'email', 'message' => "Emailul este invalid!"), 
      ); 
     } 
} 

,這是控制器

public function accessRules() 
    { 
     return array(
      array('allow', 
       'actions'=>array('aplica', 'captcha'), 
       'users'=>array('*'), 
      ), 
     ); 
    } 
.... 
$this->render('apply', array(
      "applicant" => $model, 
      'job' => $job, 
      'region' => $region, 
      'captcha'=>array(
       'class'=>'CCaptchaAction', 
       'backColor'=>0xFFFFFF, 
      ), 
     )); 

終於視圖

<?php 

     if(CCaptcha::checkRequirements()){ ?> 

     <?php echo $form->labelEx($applicant,'verifyCode'); ?><br> 
     <?php $this->widget('CCaptcha', array('captchaAction'=>'site/captcha')); ?> <br><br> 
     <?php echo $form->textField($applicant,'verifyCode'); ?><br> 

     <?php echo $form->error($applicant,'verifyCode'); ?><br><br> 
    <?php } ?> 

回答

0

因爲我使用這些用戶模型驗證規則:

public function rules() 
{   
    return array(
     array('isActive, parentId, role, ban, isLegalEntity, organization, discount, paymentDelay, debtLimit, PaymentMethod, ShippingMethod, KnowingSource, scopeOfActivity, ShablonId, agree, Group', 'numerical', 'integerOnly'=>true), 
     array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on' => 'register'),  
     ... 
    ), 
},  

,如果你嘗試同樣的內容:array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on' => 'register')

另外,還要確保在您的控制器你定義這樣的驗證碼操作:如果我讓「上」 =>「註冊」沒關係我寫的東西cuz它通過,作爲

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

結果不起作用。我發現smth在長時間搜索後,這段代碼:<?php echo $ form-> error($ applicant,'verifyCode',array(),false,false); ?>我在視圖中添加了它正在工作。不要問我爲什麼。實際上,我不知道 –

+0

,如果我第一次寫入它是不正確的,但是在它之後,它會再次刷新自己,之後它正在工作 –