浏览代码

Adding a datetime validation method to the Validation class, closes #1021

Jose Lorenzo Rodriguez 14 年之前
父节点
当前提交
fb264d9671
共有 2 个文件被更改,包括 62 次插入0 次删除
  1. 30 0
      lib/Cake/Test/Case/Utility/ValidationTest.php
  2. 32 0
      lib/Cake/Utility/Validation.php

+ 30 - 0
lib/Cake/Test/Case/Utility/ValidationTest.php

@@ -2152,4 +2152,34 @@ class ValidationTest extends CakeTestCase {
 		$this->assertTrue(Validation::userDefined('333', $validator, 'customValidate'));
 	}
 
+/**
+ * testDatetime method
+ *
+ * @access public
+ * @return void
+ */
+    function testDatetime() {
+		$this->assertTrue(Validation::datetime('27-12-2006 01:00', 'dmy'));
+		$this->assertTrue(Validation::datetime('27-12-2006 01:00', array('dmy')));
+		$this->assertFalse(Validation::datetime('27-12-2006 1:00', 'dmy'));
+
+		$this->assertTrue(Validation::datetime('27.12.2006 1:00pm', 'dmy'));
+		$this->assertFalse(Validation::datetime('27.12.2006 13:00pm', 'dmy'));
+
+		$this->assertTrue(Validation::datetime('27/12/2006 1:00pm', 'dmy'));
+		$this->assertFalse(Validation::datetime('27/12/2006 9:00', 'dmy'));
+
+		$this->assertTrue(Validation::datetime('27 12 2006 1:00pm', 'dmy'));
+		$this->assertFalse(Validation::datetime('27 12 2006 24:00', 'dmy'));
+
+		$this->assertFalse(Validation::datetime('00-00-0000 1:00pm', 'dmy'));
+		$this->assertFalse(Validation::datetime('00.00.0000 1:00pm', 'dmy'));
+		$this->assertFalse(Validation::datetime('00/00/0000 1:00pm', 'dmy'));
+		$this->assertFalse(Validation::datetime('00 00 0000 1:00pm', 'dmy'));
+		$this->assertFalse(Validation::datetime('31-11-2006 1:00pm', 'dmy'));
+		$this->assertFalse(Validation::datetime('31.11.2006 1:00pm', 'dmy'));
+		$this->assertFalse(Validation::datetime('31/11/2006 1:00pm', 'dmy'));
+		$this->assertFalse(Validation::datetime('31 11 2006 1:00pm', 'dmy'));
+	}
+
 }

+ 32 - 0
lib/Cake/Utility/Validation.php

@@ -316,6 +316,38 @@ class Validation {
 	}
 
 /**
+ * Validates a datetime value
+ * All values matching the "date" core validation rule, and the "time" one will be valid
+ * 
+ * @param array $check Value to check
+ * @param mixed $dateFormat Format of the date part
+ * Use a string or an array of the keys below. Arrays should be passed as array('dmy', 'mdy', etc)
+ * ## Keys:
+ *
+ *	- dmy 27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slash
+ *	- mdy 12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slash
+ *	- ymd 2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash
+ *  - dMy 27 December 2006 or 27 Dec 2006
+ *	- Mdy December 27, 2006 or Dec 27, 2006 comma is optional
+ *	- My December 2006 or Dec 2006
+ * 	- my 12/2006 separators can be a space, period, dash, forward slash
+ * @param string $regex Regex for the date part. If a custom regular expression is used this is the only validation that will occur.
+ * @return boolean True if the value is valid, false otherwise
+ * @see Validation::date
+ * @see Validation::time
+ */
+	function datetime($check, $dateFormat = 'ymd', $regex = null) {
+		$valid = false;
+		$parts = explode(' ', $check);
+		if (!empty($parts) && count($parts) > 1) {
+			$time = array_pop($parts);
+			$date = implode(' ', $parts);
+			$valid = self::date($date, $dateFormat, $regex) && self::time($time);
+		}
+		return $valid;
+	}
+
+/**
  * Time validation, determines if the string passed is a valid time.
  * Validates time as 24hr (HH:MM) or am/pm ([H]H:MM[a|p]m)
  * Does not allow/validate seconds.