Added 'ym'- & 'y'-date validation

Added regex in validation
Added tests analog to 'my'-date validation tests, extended the new tests even further
This commit is contained in:
Marc Würth 2013-02-15 14:35:20 +01:00
parent 854f331094
commit 33fbace4c7
2 changed files with 44 additions and 1 deletions

View file

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

View file

@ -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) {