[limb-svn] r5537 - in 3.x/trunk/limb/validation: src src/rule tests/cases/rule

svn at limb-project.com svn at limb-project.com
Thu Apr 5 16:43:56 MSD 2007


Author: pachanga
Date: 2007-04-05 16:43:56 +0400 (Thu, 05 Apr 2007)
New Revision: 5537
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5537

Added:
   3.x/trunk/limb/validation/src/rule/lmbDateRule.class.php
   3.x/trunk/limb/validation/tests/cases/rule/lmbDateRuleTest.class.php
Modified:
   3.x/trunk/limb/validation/src/lmbErrorList.class.php
   3.x/trunk/limb/validation/src/lmbValidator.class.php
   3.x/trunk/limb/validation/src/rule/lmbAtleastOneFieldRequiredRule.class.php
   3.x/trunk/limb/validation/tests/cases/rule/lmbAtleastOneFieldRequiredRuleTest.class.php
Log:
-- initial lmbDateRule added
-- lmbErrorList pretty printing added for field names
-- lmbValidator :: addAtLeastOneRequiredRule() added
-- lmbAtleastOneFieldRequiredRule accepts fields array in constructor as well

Modified: 3.x/trunk/limb/validation/src/lmbErrorList.class.php
===================================================================
--- 3.x/trunk/limb/validation/src/lmbErrorList.class.php	2007-04-05 11:42:44 UTC (rev 5536)
+++ 3.x/trunk/limb/validation/src/lmbErrorList.class.php	2007-04-05 12:43:56 UTC (rev 5537)
@@ -103,7 +103,7 @@
 
       foreach($error->getFields() as $key => $fieldName)
       {
-        $replacement = $this->getFieldName($fieldName);
+        $replacement = '"' . $this->getFieldName($fieldName) . '"';
         $text = str_replace('{' . $key . '}', $replacement, $text);
       }
 

Modified: 3.x/trunk/limb/validation/src/lmbValidator.class.php
===================================================================
--- 3.x/trunk/limb/validation/src/lmbValidator.class.php	2007-04-05 11:42:44 UTC (rev 5536)
+++ 3.x/trunk/limb/validation/src/lmbValidator.class.php	2007-04-05 12:43:56 UTC (rev 5537)
@@ -79,6 +79,12 @@
                                  array($field)));
   }
 
+  function addAtLeastOneRequiredRule($fields)
+  {
+    $this->addRule(new lmbHandle('limb/validation/src/rule/lmbAtleastOneFieldRequiredRule',
+                                 array($fields)));
+  }
+
   /**
   * Alias for adding lmbRequiredObjectRule to validator
   * @return void

Modified: 3.x/trunk/limb/validation/src/rule/lmbAtleastOneFieldRequiredRule.class.php
===================================================================
--- 3.x/trunk/limb/validation/src/rule/lmbAtleastOneFieldRequiredRule.class.php	2007-04-05 11:42:44 UTC (rev 5536)
+++ 3.x/trunk/limb/validation/src/rule/lmbAtleastOneFieldRequiredRule.class.php	2007-04-05 12:43:56 UTC (rev 5537)
@@ -32,7 +32,12 @@
   */
   function __construct()
   {
-    $this->field_names = func_get_args();
+    $args = func_get_args();
+
+    if(is_array($args[0]))
+      $this->field_names = $args[0];
+    else
+      $this->field_names = $args;
   }
 
   /**

Added: 3.x/trunk/limb/validation/src/rule/lmbDateRule.class.php
===================================================================
--- 3.x/trunk/limb/validation/src/rule/lmbDateRule.class.php	                        (rev 0)
+++ 3.x/trunk/limb/validation/src/rule/lmbDateRule.class.php	2007-04-05 12:43:56 UTC (rev 5537)
@@ -0,0 +1,56 @@
+<?php
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id: lmbEmailRule.class.php 5413 2007-03-29 10:08:00Z pachanga $
+ * @package    validation
+ */
+lmb_require('limb/validation/src/rule/lmbSingleFieldRule.class.php');
+lmb_require('limb/datetime/src/lmbDate.class.php');
+
+/**
+* Checks that field value is a valid date
+*/
+class lmbDateRule extends lmbSingleFieldRule
+{
+  const TYPE_ISO = 1;
+
+  protected $type;
+
+  function __construct($name, $type = lmbDateRule :: TYPE_ISO)
+  {
+    parent :: __construct($name);
+    $this->type = $type;
+  }
+
+  function check($value)
+  {
+    if($this->type == lmbDateRule :: TYPE_ISO)
+    {
+      try
+      {
+        new lmbDate((string)$value);
+      }
+      catch(lmbException $e)
+      {
+        $this->error(lmb_i18n('{Field} is not valid ISO format date(YYYY-MM-DD HH:MM).', 'validation'));
+      }
+    }
+  }
+
+  protected function _checkUser($value)
+  {
+    if (!preg_match('/^[a-z0-9]+([_.-][a-z0-9]+)*$/i', $value))
+        $this->error(lmb_i18n('Invalid user in {Field}.', 'validation'));
+  }
+
+  protected function _checkDomain($value)
+  {
+    parent :: check($value);
+  }
+}
+?>
\ No newline at end of file

Modified: 3.x/trunk/limb/validation/tests/cases/rule/lmbAtleastOneFieldRequiredRuleTest.class.php
===================================================================
--- 3.x/trunk/limb/validation/tests/cases/rule/lmbAtleastOneFieldRequiredRuleTest.class.php	2007-04-05 11:42:44 UTC (rev 5536)
+++ 3.x/trunk/limb/validation/tests/cases/rule/lmbAtleastOneFieldRequiredRuleTest.class.php	2007-04-05 12:43:56 UTC (rev 5537)
@@ -50,6 +50,17 @@
     $rule->validate($dataspace, $this->error_list);
   }
 
+  function testValidWithConstructorAcceptingArrayOfFields()
+  {
+    $dataspace = new lmbDataspace(array('field3' => 'whatever'));
+
+    $rule = new lmbAtleastOneFieldRequiredRule(array('field1', 'field2', 'field3'));
+
+    $this->error_list->expectNever('addError');
+
+    $rule->validate($dataspace, $this->error_list);
+  }
+
   function testInvalidAndMoreFields()
   {
     $dataspace = new lmbDataspace();

Added: 3.x/trunk/limb/validation/tests/cases/rule/lmbDateRuleTest.class.php
===================================================================
--- 3.x/trunk/limb/validation/tests/cases/rule/lmbDateRuleTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/validation/tests/cases/rule/lmbDateRuleTest.class.php	2007-04-05 12:43:56 UTC (rev 5537)
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id: lmbEmailRuleTest.class.php 5413 2007-03-29 10:08:00Z pachanga $
+ * @package    validation
+ */
+require_once(dirname(__FILE__) . '/lmbValidationRuleTestCase.class.php');
+lmb_require('limb/validation/src/rule/lmbDateRule.class.php');
+
+class lmbDateRuleTest extends lmbValidationRuleTestCase
+{
+  function testValidForISO()
+  {
+    $rule = new lmbDateRule('testfield');
+
+    $dataspace = new lmbDataspace();
+    $dataspace->set('testfield', '2007-01-12 12:30');
+
+    $this->error_list->expectNever('addError');
+
+    $rule->validate($dataspace, $this->error_list);
+  }
+
+  function testInvalidForISO()
+  {
+    $rule = new lmbDateRule('testfield');
+
+    $dataspace = new lmbDataspace();
+    $dataspace->set('testfield', 'blah 12:30');
+
+    $this->error_list->expectOnce('addError');
+
+    $rule->validate($dataspace, $this->error_list);
+  }
+
+}
+
+?>
\ No newline at end of file



More information about the limb-svn mailing list