Author: garak
Date: 2010-04-08 14:05:58 +0200 (Thu, 08 Apr 2010)
New Revision: 29045

Added:
   
plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaDoctrineForm.class.php
   plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaPropelForm.class.php
Modified:
   plugins/sfReCaptchaPlugin/branches/1.3/README
   plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaForm.class.php
Log:
[sfReCaptchaPlugin] refactored main form; added form for Propel and Doctrine


Modified: plugins/sfReCaptchaPlugin/branches/1.3/README
===================================================================
--- plugins/sfReCaptchaPlugin/branches/1.3/README       2010-04-08 11:48:52 UTC 
(rev 29044)
+++ plugins/sfReCaptchaPlugin/branches/1.3/README       2010-04-08 12:05:58 UTC 
(rev 29045)
@@ -81,9 +81,68 @@
       recaptcha:
         active:     false
 
-Now you can put the following code in the `configure()` method of your form:
+>**NOTE** It's important to make recaptcha *not* active in your test 
environment.
+>Otherwise, your tests will fail, since reCAPTCHA is not testable.
 
+
+Examples
+--------
+
+You can see an example module by enabling `recaptcha` in your applications 
`settings.yml`:
+
+    [yml]
+    all:
+      .settings:
+        enabled_modules:  [recaptcha]
+
+and clear your cache afterwards:
+
+    $ symfony cc
+
+Now you can see an example of reCAPTCHA by navigating to:
+
+    http://foobar.com/frontend_dev.php/recaptcha
+
+or for Mailhide example, to:
+
+    http://foobar.com/frontend_dev.php/recaptcha/mailhide
+
+
+Real world use
+--------------
+
+To use reCAPTCHA in your forms, simply extend `reCaptchaForm` instead of 
`BaseForm`:
+
     [php]
+    class myForm extends reCaptchaForm
+    {
+      public function configure()
+      {
+        // your definitions here...
+
+        parent::configure();
+      }
+    }
+
+Please pay attention to insert `parent::configure()` in your form, otherwise
+you'll get an error.
+
+Then, you can put in your template, where you want your CAPTCHA widget:
+
+    [php]
+    <?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 ?>
+
+If you are using a form bound to an ORM, you should extend reCaptchaPropelForm
+or reCaptchaDoctrineForm instead of reCaptchaForm.
+
+If you can't modify form extension, you need a bit more work.
+Put this code in the `configure()` method of your form:
+
+    [php]
     // reCaptcha
     if (sfConfig::get('app_recaptcha_active', false))
     {
@@ -95,7 +154,7 @@
       $this->validatorSchema->setOption('filter_extra_fields', false);
     }
 
-The following code in your action:
+And the following code in your action:
 
     [php]
     if ($request->isMethod(sfRequest::POST))
@@ -113,7 +172,7 @@
       }
     }
 
-And the following code in your template:
+Finally, the template is just the same seen above:
 
     [php]
     <?php use_helper('recaptcha') ?>
@@ -122,8 +181,10 @@
     <?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.
 
+Issue with iframe
+-----------------
+
 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`:

Added: 
plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaDoctrineForm.class.php
===================================================================
--- 
plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaDoctrineForm.class.php 
                            (rev 0)
+++ 
plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaDoctrineForm.class.php 
    2010-04-08 12:05:58 UTC (rev 29045)
@@ -0,0 +1,61 @@
+<?php
+/**
+ * reCaptchaDoctrineForm
+ *
+ * Adds widgets and validators for reCaptcha to a Doctrine form.
+ * Also, binds extra fields
+ *
+ * @package    symfony
+ * @subpackage form
+ * @author     Massimiliano Arione <[email protected]>
+ */
+class reCaptchaDoctrineForm extends BaseFormDoctrine
+{
+  public function configure()
+  {
+    if (sfConfig::get('app_recaptcha_active', false))
+    {
+      $this->widgetSchema['response'] = new sfWidgetFormInput();
+
+      // be gentle and don't overwrite possible existing postValidator
+      $postValidator = $this->validatorSchema->getPostValidator();
+      if ($postValidator)
+      {
+        $postValidators = array($postValidator, new 
sfValidatorSchemaReCaptcha('challenge', 'response'));
+        $this->validatorSchema->setPostValidator(
+          new sfValidatorAnd($postValidators)
+        );
+      }
+      else
+      {
+        $this->validatorSchema->setPostValidator(
+          new sfValidatorSchemaReCaptcha('challenge', 'response')
+        );
+      }
+
+      $this->validatorSchema->setOption('allow_extra_fields', true);
+      $this->validatorSchema->setOption('filter_extra_fields', false);
+    }
+  }
+
+  /**
+   * Binds the form with input values, adding recaptcha values
+   * @param array $taintedValues  An array of input values
+   * @param array $taintedFiles   An array of uploaded files (in the $_FILES 
or $_GET format)
+   */
+  public function bind(array $taintedValues = null, array $taintedFiles = null)
+  {
+    if (sfConfig::get('app_recaptcha_active', false))
+    {
+      $request = sfContext::getInstance()->getRequest();
+      $taintedValues['challenge'] = 
$request->getParameter('recaptcha_challenge_field');
+      $taintedValues['response'] = 
$request->getParameter('recaptcha_response_field');
+    }
+    parent::bind($taintedValues, $taintedFiles);
+  }
+
+  public function getModelName()
+  {
+  }
+
+}

Modified: 
plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaForm.class.php
===================================================================
--- plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaForm.class.php     
2010-04-08 11:48:52 UTC (rev 29044)
+++ plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaForm.class.php     
2010-04-08 12:05:58 UTC (rev 29045)
@@ -1,16 +1,57 @@
 <?php
-
-class reCaptchaForm extends sfForm
+/**
+ * reCaptchaForm
+ *
+ * Adds widgets and validators for reCaptcha.
+ * Also, binds extra fields
+ *
+ * @package    symfony
+ * @subpackage form
+ * @author     Massimiliano Arione <[email protected]>
+ */
+class reCaptchaForm extends BaseForm
 {
   public function configure()
   {
-    $this->setWidget('response', new sfWidgetFormInput());
+    if (sfConfig::get('app_recaptcha_active', false))
+    {
+      $this->widgetSchema['response'] = new sfWidgetFormInput();
 
+      // be gentle and don't overwrite possible existing postValidator
+      $postValidator = $this->validatorSchema->getPostValidator();
+      if ($postValidator)
+      {
+        $postValidators = array($postValidator, new 
sfValidatorSchemaReCaptcha('challenge', 'response'));
+        $this->validatorSchema->setPostValidator(
+          new sfValidatorAnd($postValidators)
+        );
+      }
+      else
+      {
     $this->validatorSchema->setPostValidator(
       new sfValidatorSchemaReCaptcha('challenge', 'response')
     );
+      }
 
     $this->validatorSchema->setOption('allow_extra_fields', true);
     $this->validatorSchema->setOption('filter_extra_fields', false);
   }
 }
+
+  /**
+   * Binds the form with input values, adding recaptcha values
+   * @param array $taintedValues  An array of input values
+   * @param array $taintedFiles   An array of uploaded files (in the $_FILES 
or $_GET format)
+   */
+  public function bind(array $taintedValues = null, array $taintedFiles = null)
+  {
+    if (sfConfig::get('app_recaptcha_active', false))
+    {
+      $request = sfContext::getInstance()->getRequest();
+      $taintedValues['challenge'] = 
$request->getParameter('recaptcha_challenge_field');
+      $taintedValues['response'] = 
$request->getParameter('recaptcha_response_field');
+    }
+    parent::bind($taintedValues, $taintedFiles);
+  }
+
+}

Added: 
plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaPropelForm.class.php
===================================================================
--- 
plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaPropelForm.class.php   
                            (rev 0)
+++ 
plugins/sfReCaptchaPlugin/branches/1.3/lib/form/reCaptchaPropelForm.class.php   
    2010-04-08 12:05:58 UTC (rev 29045)
@@ -0,0 +1,61 @@
+<?php
+/**
+ * reCaptchaPropelForm
+ *
+ * Adds widgets and validators for reCaptcha to a Propel form.
+ * Also, binds extra fields
+ *
+ * @package    symfony
+ * @subpackage form
+ * @author     Massimiliano Arione <[email protected]>
+ */
+class reCaptchaPropelForm extends BaseFormPropel
+{
+  public function configure()
+  {
+    if (sfConfig::get('app_recaptcha_active', false))
+    {
+      $this->widgetSchema['response'] = new sfWidgetFormInput();
+
+      // be gentle and don't overwrite possible existing postValidator
+      $postValidator = $this->validatorSchema->getPostValidator();
+      if ($postValidator)
+      {
+        $postValidators = array($postValidator, new 
sfValidatorSchemaReCaptcha('challenge', 'response'));
+        $this->validatorSchema->setPostValidator(
+          new sfValidatorAnd($postValidators)
+        );
+      }
+      else
+      {
+        $this->validatorSchema->setPostValidator(
+          new sfValidatorSchemaReCaptcha('challenge', 'response')
+        );
+      }
+
+      $this->validatorSchema->setOption('allow_extra_fields', true);
+      $this->validatorSchema->setOption('filter_extra_fields', false);
+    }
+  }
+
+  /**
+   * Binds the form with input values, adding recaptcha values
+   * @param array $taintedValues  An array of input values
+   * @param array $taintedFiles   An array of uploaded files (in the $_FILES 
or $_GET format)
+   */
+  public function bind(array $taintedValues = null, array $taintedFiles = null)
+  {
+    if (sfConfig::get('app_recaptcha_active', false))
+    {
+      $request = sfContext::getInstance()->getRequest();
+      $taintedValues['challenge'] = 
$request->getParameter('recaptcha_challenge_field');
+      $taintedValues['response'] = 
$request->getParameter('recaptcha_response_field');
+    }
+    parent::bind($taintedValues, $taintedFiles);
+  }
+
+  public function getModelName()
+  {
+  }
+
+}

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/symfony-svn?hl=en.

Reply via email to