BotDetect Symfony 2 CAPTCHA Integration Quickstart

1. Installing the BotDetect Symfony CaptchaBundle

Note: If you do not have Composer yet, you can install it by following the instructions on https://getcomposer.org

If you have Git installed on your development machine, you should use the following installation procedure:

Step 1: Download CaptchaBundle using composer

Run the following command in your application's root directory:

Step 2: Enable the bundle (app/AppKernel.php):

$bundles = array(
  ...
  new Captcha\Bundle\CaptchaBundle\CaptchaBundle(),
);

Step 3: Import CaptchaBundle routing file (app/config/routing.yml):

captcha_routing:
  resource: "@CaptchaBundle/Resources/config/routing.yml"

2. Show a Captcha Challenge on the Form

a) If Symfony is a backend for your js-based frontend, perhaps you should start with a guide that describes integration from js-framework point of view: jQuery, Angular, AngularJS, or React.

b) If you are using Symfony forms, here is how to configure Captcha options, and display Captcha in your Symfony form:

Captcha configuration options (app/config/captcha.php):

<?php if (!class_exists('CaptchaConfiguration')) { return; }

// BotDetect PHP Captcha configuration options

return [
  // Captcha configuration for example page
  'ExampleCaptcha' => [
    'UserInputID' => 'captchaCode',
    'ImageWidth' => 250,
    'ImageHeight' => 50,
  ],

];

Adding the captcha field type in your form:

$builder->add('captchaCode', 'captcha', array(
  'captchaConfig' => 'ExampleCaptcha'
));

3. Check User Input During Form Submission

Using the ValidCaptcha constraint to validate captcha code when a form is submitted:

namespace AppBundle\Entity;

use Captcha\Bundle\CaptchaBundle\Validator\Constraints as CaptchaAssert;

class Example
{
  /**
   * @CaptchaAssert\ValidCaptcha(
   *      message = "CAPTCHA validation failed, try again."
   * )
   */
  protected $captchaCode;

  public function getCaptchaCode()
  {
    return $this->captchaCode;
  }

  public function setCaptchaCode($captchaCode)
  {
    $this->captchaCode = $captchaCode;
  }
}

In-Depth Symfony CAPTCHA Instructions and Explanations

Detailed Symfony Captcha instructions and explanations can be found in the Symfony2 Captcha integration how to guide.

Other BotDetect PHP CAPTCHA Quickstarts

PHP CAPTCHA Laravel 5.2 CAPTCHA Symfony 3 CAPTCHA
CakePHP 3 CAPTCHA Laravel 5.1 CAPTCHA CodeIgniter 3.0 CAPTCHA
CakePHP 2 CAPTCHA Laravel 5.0 CAPTCHA  
CodeIgniter 2.2 CAPTCHA Laravel 4.2 CAPTCHA  

BotDetect Symfony CAPTCHA System Requirements

Symfony

Supported Symfony Versions:

  • Symfony 2.8.0+
  • Symfony 2.7.0+
  • Symfony 2.6.0+
  • Symfony 2.5.0+
  • Symfony 2.4.0+
  • Symfony 2.3.0+
  • Symfony 2.2.0+