From 0c72e327a468620b10df0b42ea4bbce88e7071ed Mon Sep 17 00:00:00 2001 From: Philipp Date: Thu, 16 Feb 2023 09:23:54 +0100 Subject: [PATCH] Fix default field value (#69) Co-authored-by: Yanick Witschi --- src/EventListener/LoadFormFieldListener.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/EventListener/LoadFormFieldListener.php b/src/EventListener/LoadFormFieldListener.php index 24db813..9cde67e 100644 --- a/src/EventListener/LoadFormFieldListener.php +++ b/src/EventListener/LoadFormFieldListener.php @@ -31,11 +31,15 @@ public function __invoke(Widget $widget, string $formId, array $formData, Form $ // We only prefill the value if it was not submitted in this step. // If you submit a value in step 1, go to step 2, then go back to step 1 and submit a wrong value there, Contao // would display an error, but we'd prefill it again with the previous value which would make no sense. - // Moreover, we prefill with submitted data as priority (= validated submitted widget data) and otherwise fall - // back to potential previous post data which has not been validated yet (e.g. you filled in the values on step 2 - // but then navigated back) + // We prefill in the following order: + // 1. Submitted data (= validated submitted widget data) + // 2. Fall back to potentially previously post data which has not been validated yet (e.g. you filled in the values + // on step 2 but then navigated back) + // 3. The widget default value itself if (!$postData->has($widget->name)) { - $widget->value = $stepData->getSubmitted()->get($widget->name, $stepData->getOriginalPostData()->get($widget->name)); + $widget->value = $stepData->getSubmitted()->get( + $widget->name, $stepData->getOriginalPostData()->get($widget->name, $widget->value) + ); } return $widget;