sfReCaptchaPlugin - 1.3.1

Integrates reCAPTCHA service in symfony

You are currently browsing
the website for symfony 1

Visit the Symfony2 website

« Back to the Plugins Home


Forgot your password?
Create an account



advanced search
Information Readme Releases Changelog Contribute
This plugin is deprecated and is not maintained anymore. Please switch to Symfony2.
Show source

sfReCaptcha plugin

The reCAPTCHA project takes unOCRable bits of text and uses people to decipher them via CAPTCHA tests. Answers are verified by juxtaposing deciphered texts with the undeciphered. By implementing reCAPTCHA you are helping digitize books.

The sfReCaptcha plugin integrates the reCAPTCHA service in symfony. It comes with a custom validator as well as an example module which demonstrates the usage.

For more information on reCAPTCHA visit


To install the plugin, navigate to your symfony project root and run:

$ symfony plugin-install

In order to use the reCAPTCHA or Mailhide service, you first need to sign up and get an API key. You can do this here for reCAPTCHA and here for Mailhide.

If you're using a name-based virtual host like http://jobeet, the API key registration will fail because the reCAPTCHA service doesn't identify your ServerName as a valid URI.

To handle this, simply append a .tld to your ServerName variable (e.g. ServerName

Insert your API keys in your applications app.yml:

    publickey:  "foo"
    privatekey: "bar"
    publickey:  "foo"
    privatekey: "bar"


You need to enable the recaptcha module in your applications settings.yml:

    enabled_modules:        [default, recaptcha]

and clear your cache afterwards:

$ symfony cc

You can use the reCaptchaForm, like in the example provided in the example module. You can try it by navigating to:

or for Mailhide example, to:

Anyway, since CAPTCHA is not testable, you should consider a different approach. First, define another key in your app.yml, under recaptcha:

    active:     true
    publickey:  "foo"
    privatekey: "bar"
    publickey:  "foo"
    privatekey: "bar"
    active:     false

Now you can put the following code in the configure() method of your form:

// reCaptcha
if (sfConfig::get('app_recaptcha_active', false))
  $this->setWidget('response', new sfWidgetFormInput());
    new sfValidatorSchemaReCaptcha('challenge', 'response')
  $this->validatorSchema->setOption('allow_extra_fields', true);
  $this->validatorSchema->setOption('filter_extra_fields', false);

The following code in your action:

if ($request->isMethod(sfRequest::POST))
  $requestData = $request->getParameter($this->form->getName());
  if (sfConfig::get('app_recaptcha_active', false))
    $requestData['challenge'] = $this->getRequestParameter('recaptcha_challenge_field');
    $requestData['response'] = $this->getRequestParameter('recaptcha_response_field');
  if ($this->form->isValid())
    // ...

And the following code in your template:

<?php use_helper('recaptcha') ?>
<?php if (sfConfig::get('app_recaptcha_active', false)): ?>
<?php echo recaptcha_get_html(sfConfig::get('app_recaptcha_publickey'), $form['response']->getError()) ?>
<?php endif ?>

So, you can safely test your forms, without worrying about CAPTCHAs.

If you need an output compliant with XHTML strict, you can pass an optional 4th parameter to recaptcha_get_html(), that forces use of object tag instead of the unsupported iframe:

<?php echo recaptcha_get_html(sfConfig::get('app_recaptcha_publickey'), $form['response']->getError(), false, true) ?>