title: CodeIgniter reCaptcha and Form_validation class
body: <p>I have a simple contact form with several required fields which is working fine. I am trying to add reCaptcha to the form for some added spam protection. </p>
  
  <p>I have the reCaptcha displaying on the form correctly, I have my keys installed. In my controller I can call the recaptcha_check_answer method and get back valid responses. I have all of the files installed and configured correctly as I can use the methods from the recaptcha class and get back good responses.</p>
  
  <p>My issue is that I would like to have an invalid reCaptcha trigger a CI form_validation error but I have spent alot of time on this and cannot get it to trigger. I am pretty new to CI so pardon my ignorance if this is something easy.</p>
  
  <p>I have tried to throw a form_validation error when the is_valid response comes back NULL. If NULL then I tried to do the following:</p>
  
  <pre><code>$this-&gt;form_validation-&gt;set_message('recaptcha_response_field', 'reCaptcha', lang('recaptcha_incorrect_response'));
  echo form_error('recaptcha_response_field');
  </code></pre>
  
  <p>Here is what the reCaptcha API responses look like for Valid and Invalid captcha's:</p>
  
  <pre><code>Valid = stdClass Object ( [0] =&gt; is_valid [1] =&gt; error [is_valid] =&gt; 1 )
  Invalid = stdClass Object ( [0] =&gt; is_valid [1] =&gt; error [is_valid] =&gt; [error] =&gt; incorrect-captcha-sol )
  </code></pre>
  
  <p>Here is the code I have in my Controller:</p>
  
  <pre><code>class Contact extends CI_Controller 
  {
  
  function __construct()
  {
    parent::__construct();
  }
  
  public function index()
  {
    $this-&gt;load-&gt;library('recaptcha');
    $this-&gt;recaptcha-&gt;set_key ('MY_PUBLIC_KEY_HERE' ,'public');
    $this-&gt;recaptcha-&gt;set_key ('MY_PRIVATE_KEY_HERE' ,'private'); // For private key
  
       // Is this a Form Submission?
       if (!empty($_POST))
       {
  
          // LOAD VALIDATION AND SET RULES
           $this-&gt;load-&gt;library('form_validation');
           $this-&gt;load-&gt;helper('form');
  
  
          $this-&gt;form_validation-&gt;set_rules('first_name', 'First Name', 'required');
          $this-&gt;form_validation-&gt;set_rules('last_name', 'Last Name', 'required');
          $this-&gt;form_validation-&gt;set_rules('email', 'Email', 'required|valid_email');
          $this-&gt;form_validation-&gt;set_rules('recaptcha_response_field', 'reCaptcha', 'required|recaptcha_check_answer');
          $this-&gt;_resp = $this-&gt;recaptcha-&gt;recaptcha_check_answer ( $this-&gt;input-&gt;ip_address(), $this-&gt;input-&gt;post('recaptcha_challenge_field',true), $this-&gt;input-&gt;post('recaptcha_response_field',true));
  
          // If the reCaptcha doesnt match throw an form_valiation Error
          if ($this-&gt;_resp-&gt;is_valid == '')
          {
              $this-&gt;form_validation-&gt;set_message('recaptcha_response_field', 'reCaptcha', lang('recaptcha_incorrect_response'));
              echo form_error('recaptcha_response_field');
          }
  
  
  
          if ($this-&gt;form_validation-&gt;run() !== FALSE)
          {
             // SUCCESS NO ERRORS - SEND EMAIL DATA AND RETURN TO HOME
                $this-&gt;load-&gt;library('email');   // LOAD EMAIL LIBRARY
                $config['protocol'] = 'sendmail';
                $config['mailpath'] = '/usr/sbin/sendmail';
                $config['charset'] = 'utf-8';
                $config['wordwrap'] = TRUE;
  
                $this-&gt;email-&gt;initialize($config);
  
                $this-&gt;email-&gt;from($this-&gt;input-&gt;post('email'), $this-&gt;input-&gt;post('first_name').' '.$this-&gt;input-&gt;post('last_name'));
                $this-&gt;email-&gt;to('jassen.michaels@gmail.com');
                $this-&gt;email-&gt;cc('');
                $this-&gt;email-&gt;bcc('');
  
                $this-&gt;email-&gt;subject('Contact Email From Spider Design Website');
                $this-&gt;email-&gt;message(
                      'First Name: '.$this-&gt;input-&gt;post('first_name')."\r\n".
                      'Last Name: '.$this-&gt;input-&gt;post('last_name')."\r\n".
                      'Company: '.$this-&gt;input-&gt;post('company')."\r\n".
                      'Location: '.$this-&gt;input-&gt;post('location')."\r\n"."\r\n".
                      'I am interested in learning: '."\r\n"."\r\n".
                      'Information Architecture: '.$this-&gt;input-&gt;post('information_architecture')."\r\n".
                      'Web Design: '.$this-&gt;input-&gt;post('web_design')."\r\n".
                      'Graphic Design: '.$this-&gt;input-&gt;post('graphic_design')."\r\n".
                      'Email Marketing: '.$this-&gt;input-&gt;post('email_marketing')."\r\n".
                      'Social Media: '.$this-&gt;input-&gt;post('social_media')."\r\n".
                      'Content Management: '.$this-&gt;input-&gt;post('content_management')."\r\n".
                      'Search Engine Optimization: '.$this-&gt;input-&gt;post('seo')."\r\n".
                      'Web Traffic Analytics: '.$this-&gt;input-&gt;post('wta')."\r\n"."\r\n".
                      'Preferred Method of Contact: '."\r\n".
                      'Phone: '.$this-&gt;input-&gt;post('phone')."\r\n".
                      'Email: '.$this-&gt;input-&gt;post('email')."\r\n"."\r\n".
                      'Comments: '."\r\n".
                      $this-&gt;input-&gt;post('comments')            
                      );
  
             //$this-&gt;email-&gt;send();
             //$this-&gt;load-&gt;helper('url');
             //redirect('http://sdi.myspiderdesign.com/fuel');
          } 
       }
  
           // Not a form submission, load view
           $this-&gt;load-&gt;helper('form');
           $this-&gt;load-&gt;view('include/spider_header');
           $this-&gt;load-&gt;view('contact');   // Content Area
           $this-&gt;load-&gt;view('include/spider_footer'); // Footer
     }
  }
  </code></pre>
  
  <p>I have commented out the redirects and contact email at the bottom of the form until I can troubleshoot this issue.</p>
  
  <p>Thanks in advance for all your help!</p>