From f5eea8dd6298fc234b37a91651ed5c84640e02a6 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 1 Feb 2020 09:30:59 +0530 Subject: [PATCH] Generate milliseconds for datatime-local inputs only if "step" is decimal. Fixes #14226 --- src/View/Widget/DateTimeWidget.php | 12 ++++++++++-- tests/TestCase/View/Helper/FormHelperTest.php | 10 +++++----- tests/TestCase/View/Widget/DateTimeWidgetTest.php | 4 ++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/View/Widget/DateTimeWidget.php b/src/View/Widget/DateTimeWidget.php index 590bb4c0f9c..53b94783c89 100644 --- a/src/View/Widget/DateTimeWidget.php +++ b/src/View/Widget/DateTimeWidget.php @@ -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', @@ -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); } /** diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index c599b8385d0..62a324b7944 100644 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -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', @@ -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', ], ]; @@ -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', ], ]; @@ -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', ], ]; @@ -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', ], ]; diff --git a/tests/TestCase/View/Widget/DateTimeWidgetTest.php b/tests/TestCase/View/Widget/DateTimeWidgetTest.php index 397f8cab23c..4e8bc9b23f0 100644 --- a/tests/TestCase/View/Widget/DateTimeWidgetTest.php +++ b/tests/TestCase/View/Widget/DateTimeWidgetTest.php @@ -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', ], ]; @@ -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', ], ];