Skip to content

Commit 9c75f5e

Browse files
committed
Normalize min/max-options for sfValidatorDateTime
1 parent 573d579 commit 9c75f5e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/validator/sfValidatorDate.class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ protected function doClean($value)
117117
else
118118
{
119119
$dateMax = new DateTime($max);
120+
$dateMax->setTimezone(new DateTimeZone(date_default_timezone_get()));
120121
$max = $dateMax->format('YmdHis');
121122
$maxError = $dateMax->format($this->getOption('date_format_range_error'));
122123
}
@@ -140,6 +141,7 @@ protected function doClean($value)
140141
else
141142
{
142143
$dateMin = new DateTime($min);
144+
$dateMin->setTimezone(new DateTimeZone(date_default_timezone_get()));
143145
$min = $dateMin->format('YmdHis');
144146
$minError = $dateMin->format($this->getOption('date_format_range_error'));
145147
}

test/unit/validator/sfValidatorDateTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
require_once(__DIR__.'/../../bootstrap/unit.php');
1212

13-
$t = new lime_test(52);
13+
$t = new lime_test(56);
1414

1515
$v = new sfValidatorDate();
1616

@@ -244,3 +244,35 @@
244244
// did it convert from the other timezone to the default timezone?
245245
$date->setTimezone($defaultTimezone);
246246
$t->is($clean, $date->format('Y-m-d H:i:s'), '->clean() respects incoming and default timezones');
247+
248+
// 'min' and 'max' options should be normalized to the default timezone
249+
$v = new sfValidatorDateTime([
250+
'min' => '2018-12-06T09:00:00Z'
251+
], [
252+
'min' => 'min',
253+
]);
254+
try
255+
{
256+
$v->clean('2018-12-06T09:59:59+01:00');
257+
$t->fail('->clean() throws an exception if the normalized date is before normalized min-option');
258+
}
259+
catch (sfValidatorError $e)
260+
{
261+
$t->pass('->clean() throws an exception if the normalized date is before normalized min-option');
262+
$t->is($e->getMessage(), 'min', '->clean() adds correct min-message');
263+
}
264+
$v = new sfValidatorDateTime([
265+
'max' => '2018-12-06T09:00:00Z'
266+
], [
267+
'max' => 'max',
268+
]);
269+
try
270+
{
271+
$v->clean('2018-12-06T10:00:01+01:00');
272+
$t->fail('->clean() throws an exception if the normalized date is after normalized max-option');
273+
}
274+
catch (sfValidatorError $e)
275+
{
276+
$t->pass('->clean() throws an exception if the normalized date is after normalized max-option');
277+
$t->is($e->getMessage(), 'max', '->clean() adds correct max-message');
278+
}

0 commit comments

Comments
 (0)