diff --git a/Classes/Form/Translator.php b/Classes/Form/Translator.php index e15472c5..54f58f04 100644 --- a/Classes/Form/Translator.php +++ b/Classes/Form/Translator.php @@ -33,7 +33,7 @@ public function __construct(FormTranslationService $service = null) * @param array $renderingOptions * @return array */ - public function translate(array $formDefinition, array $renderingOptions): array + public function translate(array $formDefinition, array $renderingOptions, array $sentValues = []): array { $result['renderables'] = []; $formRuntime = [ @@ -57,7 +57,7 @@ public function translate(array $formDefinition, array $renderingOptions): array continue; } - $pageTranslation['renderables'] = $this->translateRenderables($page['renderables'], $formRuntime); + $pageTranslation['renderables'] = $this->translateRenderables($page['renderables'], $formRuntime, $sentValues); $result['renderables'][] = array_replace_recursive($page, $pageTranslation); } @@ -68,15 +68,16 @@ public function translate(array $formDefinition, array $renderingOptions): array /** * @param array $renderables * @param array $formRuntime + * @param array sentValues * @return array */ - private function translateRenderables(array $renderables, array $formRuntime): array + private function translateRenderables(array $renderables, array $formRuntime, array $sentValues): array { foreach ($renderables as &$element) { $properties = []; if (isset($element['renderables']) && is_array($element['renderables'])) { - $element['renderables'] = $this->translateRenderables($element['renderables'], $formRuntime); + $element['renderables'] = $this->translateRenderables($element['renderables'], $formRuntime, $sentValues); } if (isset($element['validators']) && @@ -133,7 +134,8 @@ private function translateRenderables(array $renderables, array $formRuntime): a $formRuntime ); - $element['defaultValue'] = $translatedDefaultValue ?: ($element['defaultValue'] ?? ''); + $element['defaultValue'] = $translatedDefaultValue !== '' && $translatedDefaultValue !== null ? $translatedDefaultValue : ($element['defaultValue'] ?? ''); + $element['value'] = $sentValues[$element['identifier']] ?? null; $element['properties'] = $properties; } diff --git a/Classes/XClass/Controller/FormFrontendController.php b/Classes/XClass/Controller/FormFrontendController.php index 36483e73..f97b8403 100644 --- a/Classes/XClass/Controller/FormFrontendController.php +++ b/Classes/XClass/Controller/FormFrontendController.php @@ -193,7 +193,8 @@ public function renderAction(): ResponseInterface $formDefinition['i18n'] = count($i18n) ? $i18n : null; $formDefinition = $this->jsonFormTranslator->translate( $formDefinition, - $formRuntime->getFormDefinition()->getRenderingOptions() + $formRuntime->getFormDefinition()->getRenderingOptions(), + $formRuntime->getFormState() ? $formRuntime->getFormState()->getFormValues() : [] ); $formStatus['status'] = null; diff --git a/Tests/Unit/Form/TranslatorTest.php b/Tests/Unit/Form/TranslatorTest.php index 7476f9a7..f64ce755 100644 --- a/Tests/Unit/Form/TranslatorTest.php +++ b/Tests/Unit/Form/TranslatorTest.php @@ -37,6 +37,7 @@ public function test(): void 'identifier' => 'testfield', 'label' => 'test field', 'properties' => [], + 'value' => null, ], [ 'type' => 'input', @@ -47,6 +48,7 @@ public function test(): void ['code' => 111, 'message' => 'translateMe'], ] ], + 'value' => null, ], [ 'type' => 'Fieldset', @@ -61,6 +63,7 @@ public function test(): void 'properties' => [], ], ], + 'value' => null, ], [ 'type' => 'input', @@ -80,6 +83,7 @@ public function test(): void 'FERegularExpression' => '/a-z/', ] ], + 'value' => null, ], [ 'type' => 'input', @@ -87,12 +91,14 @@ public function test(): void 'label' => 'overridden field', 'properties' => [], 'renderingOptions' => ['FEOverrideType' => 'select'], + 'value' => null, ], [ 'type' => 'ImageUpload', 'identifier' => 'image', 'label' => 'Upload image', 'properties' => ['saveToFileMount' => '/upload-dir'], + 'value' => null, ] ] ], @@ -112,7 +118,8 @@ public function test(): void 'identifier' => 'testfield', 'label' => 'translatedValue', 'properties' => [], - 'defaultValue' => 'translatedValue' + 'value' => null, + 'defaultValue' => 'translatedValue', ], [ 'type' => 'input', @@ -123,7 +130,8 @@ public function test(): void ['code' => 111, 'message' => 'translatedError'], ] ], - 'defaultValue' => 'translatedValue' + 'value' => null, + 'defaultValue' => 'translatedValue', ], [ 'type' => 'Fieldset', @@ -136,10 +144,12 @@ public function test(): void 'identifier' => 'nested', 'label' => 'translatedValue', 'properties' => [], - 'defaultValue' => 'translatedValue' + 'defaultValue' => 'translatedValue', + 'value' => null, ], ], - 'defaultValue' => 'translatedValue' + 'value' => null, + 'defaultValue' => 'translatedValue', ], [ 'type' => 'input', @@ -159,7 +169,8 @@ public function test(): void 'FERegularExpression' => '/a-z/', ] ], - 'defaultValue' => 'translatedValue' + 'value' => null, + 'defaultValue' => 'translatedValue', ], [ 'type' => 'input', @@ -167,17 +178,20 @@ public function test(): void 'label' => 'translatedValue', 'properties' => [], 'renderingOptions' => ['FEOverrideType' => 'select'], - 'defaultValue' => 'translatedValue' + 'value' => null, + 'defaultValue' => 'translatedValue', ], [ 'type' => 'ImageUpload', 'identifier' => 'image', 'label' => 'translatedValue', 'properties' => ['saveToFileMount' => 'translatedValue'], + 'value' => null, 'defaultValue' => 'translatedValue', ] ], - 'label' => 'translatedValue' + 'label' => 'translatedValue', + ], ] ], $translator->translate($formDefinition, []));