Author: Kris.Wallsmith
Date: 2010-02-06 23:07:40 +0100 (Sat, 06 Feb 2010)
New Revision: 27625

Added:
   
plugins/sfFormExtraPlugin/branches/1.3/lib/validator/sfValidatorDefault.class.php
   
plugins/sfFormExtraPlugin/branches/1.3/test/validator/sfValidatorDefaultTest.php
Modified:
   plugins/sfFormExtraPlugin/branches/1.3/README
   plugins/sfFormExtraPlugin/branches/1.3/package.xml
Log:
[sfFormExtraPlugin] added sfValidatorDefault, which returns a default value 
rather than throw an error

Modified: plugins/sfFormExtraPlugin/branches/1.3/README
===================================================================
--- plugins/sfFormExtraPlugin/branches/1.3/README       2010-02-06 22:03:51 UTC 
(rev 27624)
+++ plugins/sfFormExtraPlugin/branches/1.3/README       2010-02-06 22:07:40 UTC 
(rev 27625)
@@ -45,6 +45,7 @@
   * sfValidatorReCaptcha: Validates a ReCaptcha (see sfWidgetFormReCaptcha)
   * sfValidatorBlacklist: Validates that a value is not one of the configured 
forbidden ones
   * sfValidatorSchemaTimeInterval: Validates a time interval between two dates 
provided by a widget schema
+  * sfValidatorDefault: Returns a default value rather than throwing an error
 
 Widgets
 -------

Added: 
plugins/sfFormExtraPlugin/branches/1.3/lib/validator/sfValidatorDefault.class.php
===================================================================
--- 
plugins/sfFormExtraPlugin/branches/1.3/lib/validator/sfValidatorDefault.class.php
                           (rev 0)
+++ 
plugins/sfFormExtraPlugin/branches/1.3/lib/validator/sfValidatorDefault.class.php
   2010-02-06 22:07:40 UTC (rev 27625)
@@ -0,0 +1,76 @@
+<?php
+
+/*
+ * This file is part of the symfony package.
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Returns a default value instead of throwing an error on validation failure.
+ *
+ *     $this->validatorSchema['sort'] = new sfValidatorDefault(array(
+ *       'validator' => new sfValidatorChoice(array('choices' => array('up', 
'down'))),
+ *       'default'   => 'up',
+ *     ));
+ *
+ * If no default option is provided, the supplied validator's empty value will
+ * be returned on error.
+ *
+ * @package    sfFormExtraPlugin
+ * @subpackage validator
+ * @author     Kris Wallsmith <[email protected]>
+ * @version    SVN: $Id$
+ */
+class sfValidatorDefault extends sfValidatorBase
+{
+  /**
+   * Configures the current validator.
+   *
+   * Available options:
+   *
+   *  * validator: The validator to use
+   *  * default:   The value to return if the validator fails
+   *
+   * @see sfValidatorBase
+   */
+  protected function configure($options = array(), $messages = array())
+  {
+    $this->addRequiredOption('validator');
+    $this->addOption('default', null);
+  }
+
+  /**
+   * @see sfValidatorBase
+   */
+  protected function isEmpty($value)
+  {
+    return false;
+  }
+
+  /**
+   * @see sfValidatorBase
+   *
+   * @throws InvalidArgumentException If the validator option is not a 
validator object
+   */
+  protected function doClean($value)
+  {
+    $validator = $this->getOption('validator');
+
+    if (!$validator instanceof sfValidatorBase)
+    {
+      throw new InvalidArgumentException('The "validator" option must be an 
instance of sfValidatorBase.');
+    }
+
+    try
+    {
+      return $validator->clean($value);
+    }
+    catch (sfValidatorError $error)
+    {
+      return null === $this->getOption('default') ? 
$validator->getEmptyValue() : $this->getOption('default');
+    }
+  }
+}


Property changes on: 
plugins/sfFormExtraPlugin/branches/1.3/lib/validator/sfValidatorDefault.class.php
___________________________________________________________________
Added: svn:keywords
   + Id
Added: svn:eol-style
   + native

Modified: plugins/sfFormExtraPlugin/branches/1.3/package.xml
===================================================================
--- plugins/sfFormExtraPlugin/branches/1.3/package.xml  2010-02-06 22:03:51 UTC 
(rev 27624)
+++ plugins/sfFormExtraPlugin/branches/1.3/package.xml  2010-02-06 22:07:40 UTC 
(rev 27625)
@@ -59,6 +59,7 @@
          <file role="data" name="sfValidatorBlacklist.class.php" />
          <file role="data" name="sfValidatorReCaptcha.class.php" />
          <file role="data" name="sfValidatorSchemaTimeInterval.class.php" />
+         <file role="data" name="sfValidatorDefault.class.php" />
        </dir>
        <dir name="widget">
          <file role="data" name="sfWidgetFormDoctrineChoiceGrouped.class.php" 
/>
@@ -83,6 +84,7 @@
          <file role="data" name="sfValidatorReCaptchaTest.php" />
          <file role="data" name="sfValidatorBlacklistTest.php" />
          <file role="data" name="sfValidatorSchemaTimeIntervalTest.php" />
+         <file role="data" name="sfValidatorDefaultTest.php" />
        </dir>
        <dir name="widget">
          <file role="data" name="sfWidgetFormReCaptchaTest.php" />

Added: 
plugins/sfFormExtraPlugin/branches/1.3/test/validator/sfValidatorDefaultTest.php
===================================================================
--- 
plugins/sfFormExtraPlugin/branches/1.3/test/validator/sfValidatorDefaultTest.php
                            (rev 0)
+++ 
plugins/sfFormExtraPlugin/branches/1.3/test/validator/sfValidatorDefaultTest.php
    2010-02-06 22:07:40 UTC (rev 27625)
@@ -0,0 +1,35 @@
+<?php
+
+/*
+ * This file is part of the symfony package.
+ * (c) Fabien Potencier <[email protected]>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+require_once dirname(__FILE__).'/../bootstrap.php';
+require_once 
dirname(__FILE__).'/../../lib/validator/sfValidatorDefault.class.php';
+
+$t = new lime_test(4);
+
+// ->clean()
+$t->diag('->clean()');
+
+$validator = new sfValidatorDefault(array(
+  'validator' => new sfValidatorString(),
+  'default'   => '==DEFAULT==',
+));
+$t->is($validator->clean('foo'), 'foo', '->clean() returns cleaned values');
+$t->is($validator->clean(null), '==DEFAULT==', '->clean() returns the default 
on validation failure');
+
+$validator = new sfValidatorDefault(array(
+  'validator' => new sfValidatorString(array('empty_value' => '==EMPTY==')),
+));
+$t->is($validator->clean(null), '==EMPTY==', '->clean() returns the validator 
empty value if no default value is set');
+
+$validator = new sfValidatorDefault(array(
+  'validator' => new sfValidatorString(array('empty_value' => '==EMPTY==')),
+  'default'   => '==DEFAULT==',
+));
+$t->is($validator->clean(null), '==DEFAULT==', '->clean() returns the default 
if both default and the embedded empty value are set');


Property changes on: 
plugins/sfFormExtraPlugin/branches/1.3/test/validator/sfValidatorDefaultTest.php
___________________________________________________________________
Added: svn:keywords
   + Id
Added: svn:eol-style
   + native

-- 
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