umutau  
          
              
                August 4, 2010,  7:59am
               
              1 
           
         
        
          Hi,
I try to do CCaptcha Validation but it always return me true even it is wrong.
I have this code in my view file
<?php $this->widget('CCaptcha', array(
	    'captchaAction'=>'captcha',
	    'showRefreshButton'=>true,
		'id'=>'ticketCaptcha'
		));
	?>
<?php echo CHtml::textField('verifyCode'); ?>
this is in my modele
public function rules()
	{
		return array(
			array('verifyCode', 'captcha', 'on'=>'insert', 'allowEmpty'=>!Yii::app()->user->isGuest || !extension_loaded('gd')),
		);
	}
and this is in my controller
$site=new site();
      $site->verifyCode=$_POST['verifyCode'];
      echo $site->validate();
Is there any advice, what i am doing is wrong ?
         
        
           
         
            
       
      
        
          
          
            GDzyne  
          
              
                August 4, 2010, 11:34am
               
              2 
           
         
        
          Your validation rule is set only to the scenario ‘insert’ so you would need to do something like this :
$site=new site('insert');
$site->verifyCode=$_POST['verifyCode'];
echo $site->validate();
 
        
           
         
            
       
      
        
          
          
            umutau  
          
              
                August 5, 2010, 12:26am
               
              3 
           
         
        
          It didn’t make difference. Actually I am not  inserting or updating database. The form just send email.
         
        
           
         
            
       
      
        
          
          
            GDzyne  
          
              
                August 5, 2010, 10:21am
               
              4 
           
         
        
          Calling validate() still checks against your validation rules.
There are a few things to check.
have you specified the $verifyCode public property in your model?
 
Do you have a beforeValidate() method in your model?
 
Instead of echoing  ‘$site->validate()’ what is the output of ’ print_r($site->getErrors()); ’
 
 
eg:
$site=new site('insert');
$site->verifyCode=$_POST['verifyCode'];
$site->validate();
print_r($site->getErrors());
 
        
           
         
            
       
      
        
          
          
            umutau  
          
              
                August 6, 2010, 12:56am
               
              5 
           
         
        
          $site->getErrors() returns empty array.
What should i have inside beforeValidate() function. I just add like this
protected function beforeValidate()
	{
		return true;
	}
 
        
           
         
            
       
      
        
          
          
            GDzyne  
          
              
                August 6, 2010,  8:15am
               
              6 
           
         
        
          Do you require the beforeValidate() method? If so you need to invoke the parent method. If not you should remove the method from your model class.
protected function beforeValidate()
{
   ............
   return parent::beforeValidate();
}
 
        
           
         
            
       
      
        
          
          
            umutau  
          
              
                August 8, 2010, 11:55pm
               
              7 
           
         
        
          I don’t really require this method. But even I invoke the parent method, it returns same. There is no error, even though i entered wrong character to test.
Any help please
         
        
           
         
            
       
      
        
          
          
            GDzyne  
          
              
                August 10, 2010,  6:31pm
               
              8 
           
         
        
          mmm… running out of ideas here 
Try dump your session array and see what the value of ‘captcha’ is?
         
        
           
         
            
       
      
        
          
          
            umutau  
          
              
                August 10, 2010, 11:56pm
               
              9 
           
         
        
          Is it saving into Session ohhh that’s great to know. I didn’t think of this. Thanks it solves my problem
         
        
           
         
            
       
      
        
          
          
            GDzyne  
          
              
                August 11, 2010,  9:31am
               
              10 
           
         
        
          Great that the problem is solved!