From 6393cd36f2e0de2ce45dc5da95e07c1243ab1daf Mon Sep 17 00:00:00 2001 From: Kamron Brooks <kamron@skyspider.com.au> Date: Wed, 16 Feb 2022 00:57:16 +1100 Subject: [PATCH] Pre-process date values Given that a form is opened with Form::model When using Form::date to create a date field and leaving the value null to fetch the value from the model, when the value is a Carbon then the value is set as Y-m-d H:i:s and the appropriate date is not selected. If you pass a carbon directly to the value (instead of null) then it is correctly formatted to Y-m-d. This pull request pre-processes the value when it's null using getValueAttribute. Additionally, it replaces DateTime with DateTimeInterface to allow the use of CarbonImmutable --- src/FormBuilder.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 7c0dfe5..8fec997 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -431,7 +431,9 @@ public function number($name, $value = null, $options = []) */ public function date($name, $value = null, $options = []) { - if ($value instanceof DateTime) { + $value ??= $this->getValueAttribute($name, $value); + + if ($value instanceof DateTimeInterface) { $value = $value->format('Y-m-d'); }