2013-12-19 7 views
5

Ich versuche, ein Captcha mit yii zu meinem Kontaktformular hinzuzufügen, aber es gibt ein Problem mit der Validierung.yii captcha nicht richtig validieren

Mein Modell

class ContactForm extends CFormModel 
{ 
    public $verifyCode; 

public function rules() 
{ 
    return array(

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

-Code in meinem Controller

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)); 
    } 
} 
} 

-Code in meiner advertise.php Ansicht

<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> 

Das Problem ist, dass $model->validate() gibt false zurück, wenn richtigen Code eingegeben in . $model->getErrors() gibt immer "Bestätigungscode ist falsch" zurück.

+0

Gibt es eine Möglichkeit, dieses Problem zu debuggen irgendwie? – Aditya

+0

Ihr Modell ist leer und validate gibt false zurück –

+0

ist 'contact' der Name des Formulars? Ich speichere keinen Wert in der Datenbank – Aditya

Antwort

2

Ihr Modell ist leer, weil Sie das nicht irgendeinen Wert $model->attriburtes

Sie bestanden hat:

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