Browse Source

Relax the 'my' and 'ym' date validation formats.

All other formats that include 'y' allow 2006 or 06. These formats are
now conformant with the other formats available.

Closes #2436
mark_story 12 years ago
parent
commit
e15d1652ed
2 changed files with 18 additions and 13 deletions
  1. 13 9
      lib/Cake/Test/Case/Utility/ValidationTest.php
  2. 5 4
      lib/Cake/Utility/Validation.php

+ 13 - 9
lib/Cake/Test/Case/Utility/ValidationTest.php

@@ -1415,14 +1415,16 @@ class ValidationTest extends CakeTestCase {
  * @return void
  */
 	public function testDateMyNumeric() {
-		$this->assertTrue(Validation::date('12/2006', array('my')));
+		$this->assertTrue(Validation::date('01/2006', array('my')));
 		$this->assertTrue(Validation::date('12-2006', array('my')));
 		$this->assertTrue(Validation::date('12.2006', array('my')));
 		$this->assertTrue(Validation::date('12 2006', array('my')));
-		$this->assertFalse(Validation::date('12/06', array('my')));
-		$this->assertFalse(Validation::date('12-06', array('my')));
-		$this->assertFalse(Validation::date('12.06', array('my')));
-		$this->assertFalse(Validation::date('12 06', array('my')));
+		$this->assertTrue(Validation::date('01/06', array('my')));
+		$this->assertTrue(Validation::date('12-06', array('my')));
+		$this->assertTrue(Validation::date('12.06', array('my')));
+		$this->assertTrue(Validation::date('12 06', array('my')));
+		$this->assertFalse(Validation::date('13 06', array('my')));
+		$this->assertFalse(Validation::date('13 2006', array('my')));
 	}
 
 /**
@@ -1438,12 +1440,14 @@ class ValidationTest extends CakeTestCase {
 		$this->assertTrue(Validation::date('2006 12', array('ym')));
 		$this->assertTrue(Validation::date('1900-01', array('ym')));
 		$this->assertTrue(Validation::date('2153-01', array('ym')));
+		$this->assertTrue(Validation::date('06/12', array('ym')));
+		$this->assertTrue(Validation::date('06-12', array('ym')));
+		$this->assertTrue(Validation::date('06-12', array('ym')));
+		$this->assertTrue(Validation::date('06 12', array('ym')));
 		$this->assertFalse(Validation::date('2006/12 ', array('ym')));
 		$this->assertFalse(Validation::date('2006/12/', array('ym')));
-		$this->assertFalse(Validation::date('06/12', array('ym')));
-		$this->assertFalse(Validation::date('06-12', array('ym')));
-		$this->assertFalse(Validation::date('06-12', array('ym')));
-		$this->assertFalse(Validation::date('06 12', array('ym')));
+		$this->assertFalse(Validation::date('06/12 ', array('ym')));
+		$this->assertFalse(Validation::date('06/13 ', array('ym')));
 	}
 
 /**

+ 5 - 4
lib/Cake/Utility/Validation.php

@@ -290,8 +290,8 @@ class Validation {
  * 	            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
- * 	            ym 2006/12 separators can be a space, period, dash, forward slash
+ * 	            my 12/2006 or 12/06 separators can be a space, period, dash, forward slash
+ * 	            ym 2006/12 or 06/12 separators can be a space, period, dash, forward slash
  * 	            y 2006 just the year without any separators
  * @param string $regex If a custom regular expression is used this is the only validation that will occur.
  * @return boolean Success
@@ -304,6 +304,7 @@ class Validation {
 		$separator = '([- /.])';
 		$fourDigitYear = '(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))';
 		$twoDigitYear = '([0-9]{2})';
+		$year = '(?:' . $fourDigitYear . '|' . $twoDigitYear . ')';
 
 		$regex['dmy'] = '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)' .
 			$separator . '(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29' .
@@ -325,8 +326,8 @@ class Validation {
 		$regex['My'] = '%^(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)' .
 			$separator . '((1[6-9]|[2-9]\\d)\\d{2})$%';
 
-		$regex['my'] = '%^(' . $month . $separator . $fourDigitYear . ')$%';
-		$regex['ym'] = '%^(' . $fourDigitYear . $separator . $month  . ')$%';
+		$regex['my'] = '%^(' . $month . $separator . $year . ')$%';
+		$regex['ym'] = '%^(' . $year . $separator . $month  . ')$%';
 		$regex['y'] = '%^(' . $fourDigitYear . ')$%';
 
 		$format = (is_array($format)) ? array_values($format) : array($format);