Using recaptcha with Zend_Form


I was pleasantly surprised at how simple it was to integrate a good captcha solution with Zend_Form. Your form doesn’t have to extended a special object or anything; you just need to add a “captcha” element like so:

$this->addElement('captcha', 'captcha', array(
	'label' => 'Please Type:',
	'required' => true,
	'captcha' => array(
		'pubkey' => RECAPTCHA_PUBLIC_KEY,
		'privkey' => RECAPTCHA_PRIVATE_KEY,
		'captcha' => 'reCaptcha'
	)
));

Be sure to change or set the defined variables for public and private keys provided to you with your recaptcha account. You don’t have to do anything to validate if the user entered the correct letters in your controller as Zend has already taken care of that part for you. As long as you’re using Zend’s isValid() method like so:

if ($form->isValid($request->getPost())) {

Leave a comment

Your email address will not be published. Required fields are marked *