Skip to content

Commit

Permalink
Added 'ym'- & 'y'-date validation
Browse files Browse the repository at this point in the history
Added regex in validation
Added tests analog to 'my'-date validation tests, extended the new tests even further
  • Loading branch information
ravage84 committed Feb 15, 2013
1 parent 854f331 commit 33fbace
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
39 changes: 39 additions & 0 deletions lib/Cake/Test/Case/Utility/ValidationTest.php
Expand Up @@ -1424,6 +1424,45 @@ public function testDateMyNumeric() {
$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 comment has been minimized.

Copy link
@mvdriel

mvdriel Jul 25, 2014

Why is 1899 not a valid year?

This comment has been minimized.

Copy link
@dereuromark

dereuromark Jul 25, 2014

Member

That is indeed a good question. Those ranges seem to be for "humans still alive" dates.
But I agree that a year should probably cover all possible years, especially in the (recent) past.
This use case will probably be true for historic content and alike.

This comment has been minimized.

Copy link
@ravage84

ravage84 Jul 25, 2014

Author Member

Ahem... There are still people living today from 1899!
http://en.wikipedia.org/wiki/Oldest_people
:trollface:

This comment has been minimized.

Copy link
@dereuromark

dereuromark Jul 25, 2014

Member

Oh geeze. So please, someone fix it before it's too late :)
I wonder if we could make all dates valid 1000 <= OK >= 3000
It is gonna be difficult to find a good starting and end point in between?

This comment has been minimized.

Copy link
@lorenzo

lorenzo Jul 25, 2014

Member

@ravage84 don't troll your own commits!!

This comment has been minimized.

Copy link
@ravage84

ravage84 Jul 25, 2014

Author Member

@lorenzo Apart from the fact that I just adapted the existing my rule option - which I presumed at the time was considered working correctly -, I take the right to make fun of my own faults. ;-P

@dereuromark While I agree that 1899 should be a valid date, for most inputs it wouldn't be.
Except from an application selling a life insurance for those oldest pleople living today, I think a year like 1899 would be considered invalid.

This comment has been minimized.

Copy link
@ravage84

ravage84 Jul 25, 2014

Author Member

That said, may be allowing to distinguish between "normal" dates (living span >1900) and historical dates (>0 AD, perhaps?), could solve that dilemma.

This comment has been minimized.

Copy link
@dereuromark

dereuromark Jul 25, 2014

Member

Well, I guess there will always be an edge case for more than just that. Maybe in that rare case (including historical dates) it should just be implemented as user land code. And the documentation adjusted to name the borders explicitly.

This comment has been minimized.

Copy link
@mvdriel

mvdriel Jul 25, 2014

I think it is better to have a less strict date validation, because the desired borders are very application specific. I don't think there is one border that suits in general.

This comment has been minimized.

Copy link
@dereuromark

dereuromark Jul 26, 2014

Member

See #4087 for continuing the discussion.

$this->assertFalse(Validation::date('1899', array('y')));
}

/**
* Test validating dates with multiple formats
*
Expand Down
6 changes: 5 additions & 1 deletion lib/Cake/Utility/Validation.php
Expand Up @@ -293,6 +293,8 @@ public static function custom($check, $regex = null) {
* 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
*/
Expand All @@ -307,7 +309,9 @@ public static function date($check, $format = 'ymd', $regex = null) {
$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) {
Expand Down

0 comments on commit 33fbace

Please sign in to comment.