Skip to content

Commit

Permalink
[BUGFIX] Fix forms issues with recursion (#726)
Browse files Browse the repository at this point in the history
- handle possible PHP warning
  • Loading branch information
twoldanski authored May 13, 2024
1 parent 8a41e25 commit deaf287
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
5 changes: 2 additions & 3 deletions Classes/Form/Decorator/AbstractFormDefinitionDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace FriendsOfTYPO3\Headless\Form\Decorator;

use function count;
use function in_array;

abstract class AbstractFormDefinitionDecorator implements DefinitionDecoratorInterface
Expand Down Expand Up @@ -55,8 +54,8 @@ protected function handleRenderables(array $renderables): array
{
foreach ($renderables as &$element) {
if (in_array($element['type'], ['Fieldset', 'GridRow'], true) &&
is_array($element['renderables']) &&
count($element['renderables'])) {
is_array($element['renderables'] ?? []) &&
($element['renderables'] ?? []) !== []) {
$element['elements'] = $this->handleRenderables($element['renderables']);
unset($element['renderables']);
} else {
Expand Down
18 changes: 15 additions & 3 deletions Classes/Form/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,22 @@ public function translate(array $formDefinition, array $renderingOptions, array
*/
private function translateRenderables(array $renderables, array $formRuntime, array $sentValues): array
{
foreach ($renderables as &$element) {
$translated = [];

foreach ($renderables as $element) {
$properties = [];

if (isset($element['renderables']) && is_array($element['renderables'])) {
$element['renderables'] = $this->translateRenderables($element['renderables'], $formRuntime, $sentValues);
}

$validators = [];

if (isset($element['validators']) &&
is_array($element['validators'])) {
foreach ($element['validators'] as &$validator) {
foreach ($element['validators'] as $validator) {
if (!isset($validator['errorMessage'])) {
$validators[] = $validator;
continue;
}

Expand All @@ -93,7 +98,11 @@ private function translateRenderables(array $renderables, array $formRuntime, ar
$formRuntime,
is_array($validator['options'] ?? null) ? $validator['options'] : []
);

$validators[] = $validator;
}

$element['validators'] = $validators;
}

if (isset($element['properties']) && is_array($element['properties'])) {
Expand All @@ -118,6 +127,7 @@ private function translateRenderables(array $renderables, array $formRuntime, ar
$error['code'],
$formRuntime
),
'customMessage' => $error['message'],
];
}
}
Expand All @@ -137,8 +147,10 @@ private function translateRenderables(array $renderables, array $formRuntime, ar
$element['defaultValue'] = $translatedDefaultValue !== '' && $translatedDefaultValue !== null ? $translatedDefaultValue : ($element['defaultValue'] ?? '');
$element['value'] = $sentValues[$element['identifier']] ?? null;
$element['properties'] = $properties;

$translated[] = $element;
}

return $renderables;
return $translated;
}
}
20 changes: 10 additions & 10 deletions Tests/Unit/Form/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function test(): void
'properties' => [
'validationErrorMessages' => [
['code' => 111, 'message' => 'translateMe'],
]
],
],
'value' => null,
],
Expand Down Expand Up @@ -81,7 +81,7 @@ public function test(): void
'identifier' => 'RegularExpression',
'options' => ['regularExpression' => '/a-b/'],
'FERegularExpression' => '/a-z/',
]
],
],
'value' => null,
],
Expand All @@ -99,8 +99,8 @@ public function test(): void
'label' => 'Upload image',
'properties' => ['saveToFileMount' => '/upload-dir'],
'value' => null,
]
]
],
],
],
1 => [],
],
Expand All @@ -126,9 +126,9 @@ public function test(): void
'identifier' => 'testfield',
'label' => 'translatedValue',
'properties' => [
'validationErrorMessages' => [
['code' => 111, 'message' => 'translatedError'],
]
'validationErrorMessages' => [
['code' => 111, 'message' => 'translatedError', 'customMessage' => 'translateMe'],
],
],
'value' => null,
'defaultValue' => 'translatedValue',
Expand Down Expand Up @@ -167,7 +167,7 @@ public function test(): void
'identifier' => 'RegularExpression',
'options' => ['regularExpression' => '/a-b/'],
'FERegularExpression' => '/a-z/',
]
],
],
'value' => null,
'defaultValue' => 'translatedValue',
Expand All @@ -188,12 +188,12 @@ public function test(): void
'properties' => ['saveToFileMount' => 'translatedValue'],
'value' => null,
'defaultValue' => 'translatedValue',
]
],
],
'label' => 'translatedValue',

],
]
],
], $translator->translate($formDefinition, []));
}
}

0 comments on commit deaf287

Please sign in to comment.