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

Duplicating form fields removes original field #336

Closed
jeroenmager1 opened this issue Sep 30, 2022 · 3 comments · Fixed by #377
Closed

Duplicating form fields removes original field #336

jeroenmager1 opened this issue Sep 30, 2022 · 3 comments · Fixed by #377
Labels

Comments

@jeroenmager1
Copy link

Q A
Bug? yes
New Feature? no
Bundle Version 2.5
Sulu Version 2.5
Browser Version Browser name and version

Actual Behavior

If you edit a Sulu form in the admin and click on the duplicate button of a form field, it deletes the original version of the form field the moment you hit the save button.

Expected Behavior

I expect that if i duplicate a form field both the original and the newly created form field keep existing after saving.

Steps to Reproduce

In a form click on the duplicate button of a form field. Click on the save button

@0xJas0n
Copy link

0xJas0n commented Mar 27, 2024

@alexander-schranz This bug occurs because the duplicated block also includes the same id and key as the original block in the POST request which in turn overrides the original (As shown in this screenshot).

image

I tried removing both fields in the FormManager.php inside the updateFields method in hopes that it generates the values itself which did NOT work. I think the fields need to be removed before the POST request gets sent.

I would appreciate any updates on this issue.

@alexander-schranz
Copy link
Member

alexander-schranz commented Mar 27, 2024

I think we need adjust here something: https://github.com/sulu/SuluFormBundle/blob/2.5/Manager/FormManager.php#L313

+        $fields = self::getValue($data, 'fields', []);
+        $existingIds = [];
+        $existingKeys = [];
+        foreach ($fields as &$fieldData) {
+             if (\in_array($field['id'] ?? null, $existingId) {
+                  \unset($field['id']);
+             }
+             if (\in_array($field['key'] ?? null, $existingKeys) {
+                  \unset($field['key']);
+             }
+             
+             if (isset($field['id'])) {
+                 $existingIds[] = $field['id'];
+             }
+             
+             if (isset($field['key'])) {
+                 $existingKeys[] = $field['key'];
+             }
+        }
-        $reservedKeys = \array_column(self::getValue($data, 'fields', []), 'key');
+        $reservedKeys = \array_column($fields, 'key');

        $counter = 0;

-        foreach (self::getValue($data, 'fields', []) as $fieldData) {
+        foreach ($fields as $fieldData) {

Then it should be correctly create a new field as key does not exist for the copied form field.

@alexander-schranz
Copy link
Member

alexander-schranz commented Apr 19, 2024

@0xJas0n @jeroenmager1 please give #377 a try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants