Skip to content

Commit 8cb8a9e

Browse files
committed
Fix single-mode date fieldtype crashing when saving with a time
The Control Panel submits a single date that includes a time as a ['date' => ..., 'time' => ...] array, but processSingle() passed it straight to Carbon::parse(), which throws a TypeError on arrays. This hit the default date field, whose default save format (Y-m-d H:i) makes formatHasTime() true regardless of time_enabled. Combine the date and time into a single string before parsing, and treat a missing date as an empty value.
1 parent 626888f commit 8cb8a9e

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/Fieldtypes/Date.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,17 @@ private function processSingle($data)
228228
return null;
229229
}
230230

231+
// If the value is an array, the CP has submitted the date and time separately.
232+
// We'll combine them into a single string before parsing, since Carbon can't
233+
// parse an array. A missing date means the field was left empty.
234+
if (is_array($data)) {
235+
if (! ($data['date'] ?? null)) {
236+
return null;
237+
}
238+
239+
$data = $data['date'].' '.(($data['time'] ?? null) ?: '00:00');
240+
}
241+
231242
return $this->processDateTime($data);
232243
}
233244

tests/Fieldtypes/DateTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,24 @@ public static function processProvider()
244244
['start' => '2012-08-29', 'end' => '2013-09-27'],
245245
['start' => '2012-08-29', 'end' => '2013-09-27'],
246246
],
247+
'single with date and time array from cp' => [
248+
'UTC',
249+
[],
250+
['date' => '2012-08-29', 'time' => '13:43'],
251+
'2012-08-29 13:43',
252+
],
253+
'single with date array and no time' => [
254+
'UTC',
255+
[],
256+
['date' => '2012-08-29', 'time' => null],
257+
'2012-08-29 00:00',
258+
],
259+
'single with empty date array' => [
260+
'UTC',
261+
[],
262+
['date' => null, 'time' => null],
263+
null,
264+
],
247265
];
248266
}
249267

0 commit comments

Comments
 (0)