Skip to content

[Form] Fix form value merging involving file upload, collection & checkbox #54324

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

Open
wants to merge 13 commits into
base: 5.4
Choose a base branch
from

Conversation

priyadi
Copy link
Contributor

@priyadi priyadi commented Mar 18, 2024

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #53359
License MIT

This is an update to the PR #53353

This fixes another value-files merging bug involving collections of a form containing a file upload field and a checkbox.

@priyadi
Copy link
Contributor Author

priyadi commented Mar 18, 2024

paging @nicolas-grekas

@@ -30,8 +30,6 @@ private function __construct()
* a form and needs to be consistent. PHP keyword `empty` cannot
* be used as it also considers 0 and "0" to be empty.
*
* @param mixed $data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep this one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fabbot insists that I remove it. I reverted the changes, but it won't pass the test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the change has not been reverted

@priyadi
Copy link
Contributor Author

priyadi commented May 12, 2024

@nicolas-grekas bump. Is there anything else I need to do before this PR can be merged?

*
* @internal
*/
class ParamFilesMerger
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of introducing this new internal class the code should be moved into private methods in FormUtil as the class is only used there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class represents a node in the tree. It is used by itself to traverse the entire tree. The task will be very complicated without a dedicated class.

https://github.com/symfony/symfony/pull/54324/files/30c95b8c9396624a98f6e44b0f83889b77112ab7#diff-5983ae1bcb319a7efa00aa62df7a2bcfaeaabd1894959ed7994d30a79f2281deR120

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have sent a PR against your fork which inlines the methods from the ParamFilesMerger class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have merged your PR

@priyadi priyadi force-pushed the dev-fix-merge-options-form branch from 4a6078e to 1cf2c5f Compare May 17, 2024 12:17
@fabpot fabpot modified the milestones: 5.4, 6.4 Dec 14, 2024
@remyvanlerberghe
Copy link

Hi ! I noticed this PR has been open for about a year now. The bug fix you've provided here could really help us out! Would it be possible to get this moving forward again? Happy to help with anything that might be needed to get this merged

@priyadi
Copy link
Contributor Author

priyadi commented Mar 20, 2025

Hi ! I noticed this PR has been open for about a year now. The bug fix you've provided here could really help us out! Would it be possible to get this moving forward again? Happy to help with anything that might be needed to get this merged

I think the only thing left to do is for a maintainer to merge this PR. In the meantime, you can help by applying the fix to your project and verify if it resolves your problem.

}

return $params;
}

/**
* @param UploadedFile|array $value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this type is wrong. The value can be anything (otherwise, the is_array check on line 122 would be totally useless btw)

$this->assertArrayHasKey(0, $data);
$this->assertArrayHasKey(1, $data);

$this->assertEquals('test', $itemsForm->get('1')->get('item')->getData());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this misses assertions on $itemsForm->get('0')->get('name') and $itemsForm->get('1')->get('file')

$this->assertArrayHasKey(1, $data);

$this->assertEquals('test', $itemsForm->get('1')->get('item')->getData());
$this->assertNotNull($itemsForm->get('0')->get('file'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this assert the form data is not null instead of asserting that the Form instance is not null ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, testMergeZeroIndexedCollection seems to have the same issue.

if (\is_array($value) && \is_array($files[$key] ?? null)) {
$params[$key] = self::mergeParamsAndFiles($value, $files[$key]);
unset($files[$key]);
$result[$key] = self::mergeAtPath($params, $files, $subPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this whole mergeAtPath here which then involves reading the data at a nested path, making the whole logic more complex ?

We could be looping over all keys (which is what seemed missing before) while still merging values by passing the values at that level instead.

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

Successfully merging this pull request may close these issues.

8 participants