Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX/FEATURE] Submitted form should return sent data to the frontend #547

Merged
merged 2 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Classes/Form/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(FormTranslationService $service = null)
* @param array<mixed> $renderingOptions
* @return array<mixed>
*/
public function translate(array $formDefinition, array $renderingOptions): array
public function translate(array $formDefinition, array $renderingOptions, array $sentValues = []): array
{
$result['renderables'] = [];
$formRuntime = [
Expand All @@ -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);
}
Expand All @@ -68,15 +68,16 @@ public function translate(array $formDefinition, array $renderingOptions): array
/**
* @param array<int, mixed> $renderables
* @param array<string, mixed> $formRuntime
* @param array<string, mixed> sentValues
* @return array<int, mixed>
*/
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']) &&
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion Classes/XClass/Controller/FormFrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 21 additions & 7 deletions Tests/Unit/Form/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function test(): void
'identifier' => 'testfield',
'label' => 'test field',
'properties' => [],
'value' => null,
],
[
'type' => 'input',
Expand All @@ -47,6 +48,7 @@ public function test(): void
['code' => 111, 'message' => 'translateMe'],
]
],
'value' => null,
],
[
'type' => 'Fieldset',
Expand All @@ -61,6 +63,7 @@ public function test(): void
'properties' => [],
],
],
'value' => null,
],
[
'type' => 'input',
Expand All @@ -80,19 +83,22 @@ public function test(): void
'FERegularExpression' => '/a-z/',
]
],
'value' => null,
],
[
'type' => 'input',
'identifier' => 'overridden',
'label' => 'overridden field',
'properties' => [],
'renderingOptions' => ['FEOverrideType' => 'select'],
'value' => null,
],
[
'type' => 'ImageUpload',
'identifier' => 'image',
'label' => 'Upload image',
'properties' => ['saveToFileMount' => '/upload-dir'],
'value' => null,
]
]
],
Expand All @@ -112,7 +118,8 @@ public function test(): void
'identifier' => 'testfield',
'label' => 'translatedValue',
'properties' => [],
'defaultValue' => 'translatedValue'
'value' => null,
'defaultValue' => 'translatedValue',
],
[
'type' => 'input',
Expand All @@ -123,7 +130,8 @@ public function test(): void
['code' => 111, 'message' => 'translatedError'],
]
],
'defaultValue' => 'translatedValue'
'value' => null,
'defaultValue' => 'translatedValue',
],
[
'type' => 'Fieldset',
Expand All @@ -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',
Expand All @@ -159,25 +169,29 @@ public function test(): void
'FERegularExpression' => '/a-z/',
]
],
'defaultValue' => 'translatedValue'
'value' => null,
'defaultValue' => 'translatedValue',
],
[
'type' => 'input',
'identifier' => 'overridden',
'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, []));
Expand Down