2013-12-19 8 views
5

yiiを使用してcaptchaを私のコンタクトフォームに追加しようとしていますが、検証に何らかの問題があります。私advertise.phpビューで私のコントローラyii captchaが正しく検証されていません

public function filters() 
{ 
    return array(
     'accessControl', 
    ); 
} 



public function accessRules() 
{ 
    return array(
      array( 'allow', //allow all users to perform advertise and index action 
        'actions' => array('advertise','index', 'captcha'), 
     'users' => array('*'), 
      ), 
    ); 
} 

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

public actionAdvertise() 
{ $model = new ContactForm; 
    $model->scenario = 'captchaRequired'; 
    if($model->validate()){ 
    //some code 
    } else { 
      $this->render('advertise', array('model' => $model)); 
    } 
} 
} 

コードで

マイモデル

class ContactForm extends CFormModel 
{ 
    public $verifyCode; 

public function rules() 
{ 
    return array(

     array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>'captchaRequired'), 
     array('verifyCode', 'safe'), 
       ); 
} 
} 

コードは

<form action="" method="post"> 
      <?php 
       $form=$this->beginWidget('CActiveForm',array(
        'id'=>'contact-form', 
        'enableAjaxValidation'=>false, 
       )); 
      ?> 
      <?php if(CCaptcha::checkRequirements()){ ?> 
       <div class="row"> 
     <div class="contact_field_wrapper"> 
       <?php echo '<b>ARE YOU HUMAN?</b><br />'.$form->labelEx($model, 'verifyCode'); ?> 
<div class="captcha user-captcha"> 
     <?php $this->widget('CCaptcha',array( 'captchaAction'=>'site/captcha'));                       
    ?> 
           <?php echo $form->error($model, 'verifyCode'); ?> 
           <?php echo '<br />'.$form->textField($model,'verifyCode'); ?> 
           <div class="hint">Please enter the letters as they are shown in the image above.<br/> 
            Letters are not case-sensitive. 
           </div> 
          </div> 
          </div> 
          </div> 
<?php } ?> 
<?php $this->endWidget(); ?> 
</form> 

問題が$model->validate() falseを返すこと、正しいコード入力にする場合であります。 $model->getErrors()は常に「確認コードが間違っています」を返しています。

+0

ありますか? – Aditya

+0

モデルが空で、検証が偽を返す –

+0

はフォームの名前に「連絡先」ですか?データベースに値を格納していません – Aditya

答えて

2

モデルは、あなたが$model->attriburtes

に任意の値を渡していなかったので、空である。この操作を行います。何とかこの問題をデバッグする方法は

if($_POST['ContactForm']) 
{ 
    $model->attributes() = $_POST['ContactForm']; 
    if($model->validate()) 
    { 
      //some code 
    } 
} 
$this->render('advertise', array('model' => $model)); 
関連する問題