Skip to content

Commit

Permalink
Make sure 12:15PM validates
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed May 29, 2015
1 parent a4ad1cb commit 252267c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Validation/Validation.php
Expand Up @@ -1037,6 +1037,9 @@ protected static function _getDateString($value)

if (isset($value['hour'])) {
if (isset($value['meridian'])) {
if ($value['hour'] === 12) {

This comment has been minimized.

Copy link
@brandon-hoffman

brandon-hoffman May 29, 2015

if (intval($value['hour']) === 12) {

should work with both form post data (string) and int values passed

This comment has been minimized.

Copy link
@dereuromark

dereuromark May 29, 2015

Member

Good point. (int) cast can be added.

$value['hour'] = 0;
}
$value['hour'] = strtolower($value['meridian']) === 'am' ? $value['hour'] : $value['hour'] + 12;
}
$value += ['minute' => 0, 'second' => 0];
Expand Down
6 changes: 6 additions & 0 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -1519,6 +1519,12 @@ public function testTimeArray()
];
$this->assertTrue(Validation::time($date));

$date = [
'hour' => 12, 'minute' => 14, 'second' => 15,
'meridian' => 'pm'
];
$this->assertTrue(Validation::time($date));

$date = [
'hour' => 'farts', 'minute' => 'farts'
];
Expand Down

4 comments on commit 252267c

@brandon-hoffman
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dereuromark I just tested the solution with post data from a form and it does not work because the value of hour comes in as a string i.e.

$value['hour'] = '12'

suggest

if ((int)$value['hour'] === 12) {
                    $value['hour'] = 0;
                }

@dereuromark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting might also make 12abc pass, so maybe we should better use

if ($value['hour'] == 12) {}

A non-strict check

Can you make a test case and provide the fix as PR maybe?

@Zuluru
Copy link
Contributor

@Zuluru Zuluru commented on 252267c Jul 24, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this isn't fixed yet? I found a similar issue in saving data, where "12" "15" "am" is saved as 12:15:00, not 00:15:00. Maybe all uses of "meridian" should be double-checked for such problems?

@markstory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Zuluru How can someone reproduce the issue you're having? If you have a way to reproduce the issue a new issue would be great.

Please sign in to comment.