Skip to content

Commit

Permalink
Generate milliseconds for datatime-local inputs only if "step" is dec…
Browse files Browse the repository at this point in the history
…imal.

Fixes #14226
  • Loading branch information
ADmad committed Feb 1, 2020
1 parent e139dcb commit f5eea8d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/View/Widget/DateTimeWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DateTimeWidget extends BasicWidget
* @var string[]
*/
protected $formatMap = [
'datetime-local' => 'Y-m-d\TH:i:s.v',
'datetime-local' => 'Y-m-d\TH:i:s',
'date' => 'Y-m-d',
'time' => 'H:i:s',
'month' => 'Y-m',
Expand Down Expand Up @@ -192,7 +192,15 @@ protected function formatDateTime($value, array $options): string
$dateTime = $dateTime->setTimezone($timezone);
}

return $dateTime->format($this->formatMap[$options['type']]);
$format = $this->formatMap[$options['type']];
if (
$options['type'] === 'datetime-local' &&
strpos((string)$options['step'], '.') !== false
) {
$format = $format . '.v';
}

return $dateTime->format($format);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/View/Helper/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3433,7 +3433,7 @@ public function testControlDatetime()
'name' => 'prueba',
'id' => 'prueba',
'type' => 'datetime-local',
'value' => '2019-09-27T02:52:43.000',
'value' => '2019-09-27T02:52:43',
'step' => '1',
],
'/div',
Expand Down Expand Up @@ -6083,7 +6083,7 @@ public function testDateTime()
'input' => [
'type' => 'datetime-local',
'name' => 'date',
'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}\.\d{3}/',
'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
'step' => '1',
],
];
Expand Down Expand Up @@ -6157,7 +6157,7 @@ public function testDatetimeWithDefault()
'input' => [
'type' => 'datetime-local',
'name' => 'updated',
'value' => '2009-06-01T11:15:30.000',
'value' => '2009-06-01T11:15:30',
'step' => '1',
],
];
Expand All @@ -6170,7 +6170,7 @@ public function testDatetimeWithDefault()
'input' => [
'type' => 'datetime-local',
'name' => 'updated',
'value' => '2009-06-01T11:15:30.000',
'value' => '2009-06-01T11:15:30',
'step' => '1',
],
];
Expand Down Expand Up @@ -7415,7 +7415,7 @@ public function testDateTimeWithGetForms()
'input' => [
'type' => 'datetime-local',
'name' => 'created',
'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}\.\d{3}/',
'value' => 'preg:/' . date('Y-m-d') . 'T\d{2}:\d{2}:\d{2}/',
'step' => '1',
],
];
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/View/Widget/DateTimeWidgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function testRenderValid($selected)
'input' => [
'type' => 'datetime-local',
'name' => '',
'value' => '2014-01-20T12:30:45.000',
'value' => '2014-01-20T12:30:45',
'step' => '1',
],
];
Expand All @@ -128,7 +128,7 @@ public function testTimezoneOption()
'input' => [
'type' => 'datetime-local',
'name' => '',
'value' => '2019-02-03T15:30:00.000',
'value' => '2019-02-03T15:30:00',
'step' => '1',
],
];
Expand Down

0 comments on commit f5eea8d

Please sign in to comment.