Recaptcha With Zend Form Element

In an earlier blog, I had written about how Zend_Form_Element_Captcha can be used to easily generate CAPTCHA’s for your website. I have since embraced “Recaptcha” as the defacto captcha adapter. The same Zend_Form_Element_Captcha can be repurposed to easily deliver it on your site with minimal effort.

Advantages of Recaptcha:

  • Endorsed (and owned) by Google.
  • It is a webservice.. No more expensive GDI operations performed on your webserver. The captcha generation is done on google servers and delivered to your webpage as Html.
  • Provides Accessibility support.
  • Has a lot of “fuzzy” logic going on.. Notice that you don’t have to type the “recaptcha” word verbatim. Missing an illegible word will not invalidate your response. Captcha generated by you will not be so forgiving.
  • Why reinvent the wheel?

The first step towards getting started is to go on the Recaptcha website and create a key pair (You need a google account to log in).

I prefer creating a “global key”.. The keys can then be used on any website (and you can type in any domain in the provided space as shown below:


Click on the “Create Key” button at the bottom of the page. You will be presented with a public key and private key. You will require these when you make your webservice calls.

Ok.. onto our form class:

class Form_Recaptcha extends Zend_Form
    public function init()
        $recaptcha = new Zend_Service_ReCaptcha("sdfgdsfgdfgzGsA5WwGrLW4dq",
        $captcha = $this->createElement('Captcha', 'ReCaptcha',

        $captcha->setLabel('Enter CAPTCHA');

        $submit = $this->createElement('submit', 'submit');

(Note that I have mangled my keys.. so please do not attempt to use the values in the code above!)

First, a Zend_Service_Recaptcha instance is created with our public and private key as parameters. This is passed in as an option to the “createElement” call.

One caveat though is that you MUST provide the options array when you create the captcha element.

For example, creating the captcha element like so:

$captcha = $this->createElement('Captcha', 'ReCaptcha');

Although Syntactically and Semantically correct, will result in the following error message:

Message: Invalid validator provided to addValidator; must be string or Zend_Validate_Interface

The indexcontroller code that exercises the above form is shown below:

class IndexController extends Zend_Controller_Action
    public function indexAction()
        // action body
        $form = new Form_Recaptcha();
        $this->view->form = $form;
        if ($this->_request->isPost())
            if ($form->isValid($_POST))
               //Checks Everything! Captcha included 🙂
                echo "Valid!";


As shown above, the isValid() call checks all form elements including the recaptcha. Could not be simpler!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s