From 555fcd65b2d3491757cde2bc303db71c0d30c7d3 Mon Sep 17 00:00:00 2001 From: Matthias Zobrist Date: Sun, 28 Apr 2024 10:28:36 +0200 Subject: [PATCH] Fixed the logic to get the field name --- src/Serializer/FormNormalizer.php | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Serializer/FormNormalizer.php b/src/Serializer/FormNormalizer.php index ddddd7a..be35971 100644 --- a/src/Serializer/FormNormalizer.php +++ b/src/Serializer/FormNormalizer.php @@ -120,13 +120,7 @@ private function parseFormData(FormInterface $form, array &$data, array $ignored return; } - $key = ''; - $parent = $form; - do { - $key = ((string) $parent->getPropertyPath()) . $key; - $parent = $parent->getParent(); - } while ($parent); - + $key = $this->getKeyForField($form); $data['formData'][$key] = $form->getData(); if ($form->isRequired()) { @@ -143,6 +137,23 @@ private function parseFormData(FormInterface $form, array &$data, array $ignored } } + private function getKeyForField(FormInterface $field): string + { + $parent = null; + if ($field->getParent()) { + $parent = $this->getKeyForField($field->getParent()); + } + + $name = (string) $field->getName(); + if ($parent) { + $key = sprintf('%s[%s]', $parent, $name); + } else { + $key = $name; + } + + return $key; + } + /** * @deprecated since Symfony 6.3, use "getSupportedTypes()" instead */