root/trunk/lib/form/sfFormReCaptcha.class.php

Revision 71, 2.8 kB (checked in by nperriault, 4 months ago)

[1.1] Application connection forms, enhanced tests and SymfoniansTestBrowser?

Line 
1 <?php
2 /*
3  * This file is part of the symfony package.
4  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
5  *
6  * For the full copyright and license information, please view the LICENSE
7  * file that was distributed with this source code.
8  */
9
10 /**
11  * sfFormReCatcha is a base form embedding a recaptcha widget and validator, it
12  * can also bind submitted parameters directly from the request using the
13  * bindFromRequest() method.
14  *
15  * You must define a private and a public recaptcha key in order to use this
16  * form. You can do so passing options to the constructor of the class, eg:
17  *
18  *     $form = new sfFormReCaptcha(array(), array('recaptcha_public_key'  => '12345',
19  *                                                'recaptcha_private_key' => '54321'));
20  *
21  * You can use symfony environements to configure if you want to use recaptcha
22  * for certain environment only
23  *
24  * @package    symfony
25  * @subpackage form
26  * @author     Nicolas Perriault <nicolas.perriault@symfony-project.com>
27  * @version    SVN: $Id$
28  */
29 class sfFormReCaptcha extends sfForm
30 {
31   /**
32    * @see sfForm
33    */
34   public function __construct($defaults = array(), $options = array(), $CSRFSecret = null)
35   {
36     parent::__construct($defaults, $options, $CSRFSecret);
37
38     // We setup the captcha now, because the configure() method has just been
39     // called in __construct() so we can append the captcha field to the whole form.
40     $this->setupCaptcha();
41   }
42
43   /**
44    * Bind parameters, including the captcha ones, from a sfRequest object instance
45    *
46    * @param  sfRequest A sfRequest instance
47    *
48    * @return Boolean   true if the form is valid, false otherwise
49    */
50   public function bindFromRequest(sfRequest $request)
51   {
52     $nameFormat = $this->widgetSchema->getNameFormat();
53
54     if ('%s' == $nameFormat)
55     {
56       throw new LogicException('You must define a name format using setNameFormat() in order to use the bindFromRequest() method');
57     }
58
59     $nameFormat = substr($nameFormat, 0, strpos($nameFormat, ('[')));
60
61     $params = $request->getParameter($nameFormat, array());
62     if (true === sfConfig::get('app_recaptcha_enabled'))
63     {
64       $captcha = array(
65         'recaptcha_challenge_field' => $request->getParameter('recaptcha_challenge_field'),
66         'recaptcha_response_field'  => $request->getParameter('recaptcha_response_field'),
67       );
68       $params = array_merge($params, array('captcha' => $captcha));
69     }
70
71     return parent::bind($params);
72   }
73
74   /**
75    * Setup captcha for current form
76    *
77    */
78   protected function setupCaptcha()
79   {
80     if (true === sfConfig::get('app_recaptcha_enabled'))
81     {
82       $this->widgetSchema['captcha'] = new sfWidgetFormReCaptcha(array('public_key' => sfConfig::get('app_recaptcha_public_key')));
83       $this->validatorSchema['captcha'] = new sfValidatorReCaptcha(array('private_key' => sfConfig::get('app_recaptcha_private_key')));
84     }
85   }
86 }
87
Note: See TracBrowser for help on using the browser.