From 33fbace4c77040acf2379bc1e9463e6f580feeef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=BCrth?= Date: Fri, 15 Feb 2013 14:35:20 +0100 Subject: [PATCH] Added 'ym'- & 'y'-date validation Added regex in validation Added tests analog to 'my'-date validation tests, extended the new tests even further --- lib/Cake/Test/Case/Utility/ValidationTest.php | 39 +++++++++++++++++++ lib/Cake/Utility/Validation.php | 6 ++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/Utility/ValidationTest.php b/lib/Cake/Test/Case/Utility/ValidationTest.php index 3cc375328..c60fe6cdd 100644 --- a/lib/Cake/Test/Case/Utility/ValidationTest.php +++ b/lib/Cake/Test/Case/Utility/ValidationTest.php @@ -1424,6 +1424,45 @@ class ValidationTest extends CakeTestCase { $this->assertFalse(Validation::date('12 06', array('my'))); } +/** + * testDateYmNumeric method + * + * @return void + */ + public function testDateYmNumeric() { + $this->assertTrue(Validation::date('2006/12', array('ym'))); + $this->assertTrue(Validation::date('2006-12', array('ym'))); + $this->assertTrue(Validation::date('2006-12', array('ym'))); + $this->assertTrue(Validation::date('2006 12', array('ym'))); + $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->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'))); + } + +/** + * testDateY method + * + * @return void + */ + public function testDateY() { + $this->assertTrue(Validation::date('1900', array('y'))); + $this->assertTrue(Validation::date('1984', array('y'))); + $this->assertTrue(Validation::date('2006', array('y'))); + $this->assertTrue(Validation::date('2008', array('y'))); + $this->assertTrue(Validation::date('2013', array('y'))); + $this->assertTrue(Validation::date('2104', array('y'))); + $this->assertFalse(Validation::date('20009', array('y'))); + $this->assertFalse(Validation::date(' 2012', array('y'))); + $this->assertFalse(Validation::date('3000', array('y'))); + $this->assertFalse(Validation::date('1899', array('y'))); + } + /** * Test validating dates with multiple formats * diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index d3b9d6464..d12998ed6 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -293,6 +293,8 @@ class Validation { * 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 + * 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 */ @@ -307,7 +309,9 @@ class Validation { $regex['dMy'] = '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ (((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]\\d)\\d{2})$/'; $regex['Mdy'] = '/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep)(tember)?|(Nov|Dec)(ember)?)\\ (0?[1-9]|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,?\\ ((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\\,?\\ ((1[6-9]|[2-9]\\d)\\d{2}))$/'; $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)?)[ /]((1[6-9]|[2-9]\\d)\\d{2})$%'; - $regex['my'] = '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))))$%'; + $regex['my'] = '%^((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9][0-9][0-9])))$%'; + $regex['ym'] = '%^((([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))([- /.])(0[123456789]|10|11|12))$%'; + $regex['y'] = '%^(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))$%'; $format = (is_array($format)) ? array_values($format) : array($format); foreach ($format as $key) {