Requirements
- Zend Framework (1.7.8 at writing time)
- Valid ReCaptcha keys (public & private)
Simple ReCaptcha
Zend Form Class
source: application/forms/ReCaptcha.php
<?php
class Form_ReCaptcha extends Zend_Form
{
public function init()
{
$this->setMethod('post');
//Add your elements here...
$recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);
$captcha = new Zend_Form_Element_Captcha('challenge',
array('captcha' => 'ReCaptcha',
'captchaOptions' => array('captcha' => 'ReCaptcha', 'service' => $recaptcha)));
$this->addElement($captcha);
// Add the submit button
$this->addElement('submit', 'submit', array('label' => 'Submit'));
}
}
?>
Zend Controller Class
source: application/controller/ReCaptchaController.php
<?php
class ReCaptchaController extends Zend_Controller_Action
{
public function indexAction()
{
require_once APPLICATION_PATH . '/forms/Contact.php';
$form = new Form_ReCaptcha();
if ($this->_request->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
$recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);
$result = $recaptcha->verify($this->_getParam('recaptcha_challenge_field'),
$this->_getParam('recaptcha_response_field'));
if (!$result->isValid()) {
//ReCaptcha validation error
//Your action here...
}
}
}
$this->view->form = $form;
}
}
?>
Customized ReCaptcha
You may also want to internationalizing or change colors to ReCaptcha, to do it you need to specify some options for the Zend_Service_ReCaptcha object.
See the ReCaptcha wiki for a complete list of available options.
Zend Form Class
source: application/forms/ReCaptcha.php
<?php
class Form_ReCaptcha extends Zend_Form
{
public function init()
{
$this->setMethod('post');
//Add your elements here...
$recaptcha = new Zend_Service_ReCaptcha($publickey, $privatekey);
//Translate in your language
$recaptcha_it_translation =
array('visual_challenge' => "Verifica video",
'audio_challenge' => "Verifica audio",
'refresh_btn' => "Effettua una nuova verifica",
'instructions_visual' => "Scrivi le due parole",
'instructions_audio' => "Scrivi quello che ascolti",
'help_btn' => "Aiuto",
'play_again' => "Riascolto di nuovo l'audio",
'cant_hear_this' => "Scarica l'audio come MP3",
'incorrect_try_again' => "Incorretto. Prova ancora.");
$recaptcha->setOption('custom_translations', $recaptcha_it_translation);
//Change theme
$recaptcha->setOption('theme', 'clean');
$captcha = new Zend_Form_Element_Captcha('challenge',
array('captcha' => 'ReCaptcha',
'captchaOptions' => array('captcha' => 'ReCaptcha',
'service' => $recaptcha)));
$this->addElement($captcha);
// Add the submit button
$this->addElement('submit', 'submit', array('label' => 'Submit'));
}
}
?>
-
Thanks youuuuuuuuuuuuuu very much.
I was looking for the translations options!
-
With 1.10.3 you not longer need the controller code as the recaptcha request would be submitted twice. Now when you build the form the recaptcha fields are valided against the captcha adapter.
All you need to do in the controller is call $form->isValid() to check the form is valid captcha and all other fields included.

4 comments
Comments feed for this article
Trackback link: http://lucasforge.2bopen.org/2009/04/recaptcha-with-zend-form/trackback/