title: Adding Captcha to Symfony2 Login
body: <p>I need to add Captcha to my login page, I am using GregwarCaptchaBundle and FosUserBundle. </p>
  
  <p>For the moment I have get show the captcha on the login using the following code:</p>
  
  <pre><code>&lt;?php
  
  /*
   * This file is part of the FOSUserBundle package.
   *
   * (c) FriendsOfSymfony &lt;http://friendsofsymfony.github.com/&gt;
   *
   * For the full copyright and license information, please view the LICENSE
   * file that was distributed with this source code.
   */
  
  namespace FOS\UserBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\Security\Core\Security;
  use Symfony\Component\Security\Core\SecurityContextInterface;
  use Symfony\Component\Security\Core\Exception\AuthenticationException;
  use Gregwar\Captcha\CaptchaBuilder;
  
  class SecurityController extends Controller
  {
      public function loginAction(Request $request)
      {
          $builtCaptcha = new CaptchaBuilder();
          $builtCaptcha-&gt;build();
          $builtCaptcha-&gt;save('captcha.jpg');
          /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
          $session = $request-&gt;getSession();
  
          if (class_exists('\Symfony\Component\Security\Core\Security')) {
              $authErrorKey = Security::AUTHENTICATION_ERROR;
              $lastUsernameKey = Security::LAST_USERNAME;
          } else {
              // BC for SF &lt; 2.6
              $authErrorKey = SecurityContextInterface::AUTHENTICATION_ERROR;
              $lastUsernameKey = SecurityContextInterface::LAST_USERNAME;
          }
  
          // get the error if any (works with forward and redirect -- see below)
          if ($request-&gt;attributes-&gt;has($authErrorKey)) {
              $error = $request-&gt;attributes-&gt;get($authErrorKey);
          } elseif (null !== $session &amp;&amp; $session-&gt;has($authErrorKey)) {
              $error = $session-&gt;get($authErrorKey);
              $session-&gt;remove($authErrorKey);
          } else {
              $error = null;
          }
  
          if (!$error instanceof AuthenticationException) {
              $error = null; // The value does not come from the security component.
          }
  
          // last username entered by the user
          $lastUsername = (null === $session) ? '' : $session-&gt;get($lastUsernameKey);
  
          if ($this-&gt;has('security.csrf.token_manager')) {
              $csrfToken = $this-&gt;get('security.csrf.token_manager')-&gt;getToken('authenticate')-&gt;getValue();
          } else {
              // BC for SF &lt; 2.4
              $csrfToken = $this-&gt;has('form.csrf_provider')
                  ? $this-&gt;get('form.csrf_provider')-&gt;generateCsrfToken('authenticate')
                  : null;
          }
          $t = $request-&gt;get('_captcha');
          if($t!=$builtCaptcha){
              echo 'error';
          }
          var_dump($t);
  
          return $this-&gt;renderLogin(array(
              'last_username' =&gt; $lastUsername,
              'error' =&gt; $error,
              'csrf_token' =&gt; $csrfToken,
              'captcha' =&gt; $builtCaptcha,
          ));
      }
  
      /**
       * Renders the login template with the given parameters. Overwrite this function in
       * an extended controller to provide additional data for the login template.
       *
       * @param array $data
       *
       * @return \Symfony\Component\HttpFoundation\Response
       */
      protected function renderLogin(array $data)
      {
          return $this-&gt;render('FOSUserBundle:Security:login.html.twig', $data);
      }
  
      public function checkAction($builtCaptcha)
      {
              return $this-&gt;redirect($this-&gt;generateUrl('fos_user_login'));
          }
          throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
      }
  
      public function logoutAction()
      {
          throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
      }
  }
  </code></pre>
  
  <p>And I overrided the login and register template as the documentation explain (the registration is working fine with the captcha)</p>
  
  <p>The problem is that I don't know how to validate the captcha's code.</p>
  
  <p>I guess that I should do it into the checkAction() </p>
  
  <p>But I am not sure, I am very caught with this problem.</p>
  
  <p>If someone could help me I would be very grateful, Thanks in advance.</p>