-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Add "reindex" option for the CollectionType #13116
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
Conversation
/** | ||
* @var bool | ||
*/ | ||
protected $reindex; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be private
Please don't change the permissions on files. the source code has no reason to be executable. If you are on Windows, you should configure Git to ignore file mode changes because Windows does not support Unix file modes and so Git detects messy changes (this is done by default when installing GitBash but other Windows Git clients may not do it for you): |
$this->assertArrayNotHasKey(1, $event->getData()); | ||
$this->assertArrayHasKey(2, $event->getData()); | ||
$this->assertArrayHasKey(3, $event->getData()); | ||
$this->assertArrayHasKey(4, $event->getData()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, you should not only assert that it has the keys, but also which values are there for each key
This PR needs to be reviewed again because the logic has been changed. |
This looks good to me, however I would prefer if @webmozart could review it as well. |
@@ -110,6 +116,29 @@ public function preSubmit(FormEvent $event) | |||
throw new UnexpectedTypeException($data, 'array or (\Traversable and \ArrayAccess)'); | |||
} | |||
|
|||
// Fix array indices | |||
if ($this->reindex) { | |||
$originalData = $data; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@webmozart I rewrote the code to support array-like objects. |
Why do we need a new option for that? Can't we always reindex? |
Thanks for your PR @sergeyfedotov! :) Unfortunately it's more complicated than that (suprise). When we reindex the data submitted by the browser, we lose the ability to assign submitted data to entries of the collection. Imagine: $data = [
0 => ['price' => 10],
2 => ['price' => 20],
];
$collection = [
0 => Product(id => 1, price => 10),
1 => Product(id => 2, price => 12),
2 => Product(id => 3, price => 15),
]; Instead of removing product with ID 2 from the list and updating product 3 to price 20, we now removed product 3 and updated product 2 (which may or may not be what we want to do). Similar issues are already described in #7468 and #7828. A proper fix, from what I can judge right now without too much investigation, would involve the following steps:
|
@webmozart thanks for the detailed answer. You are right that a proper solution is more complicated. This pull request only trying to solve a problem with the errors association. In fact is a data association problem. I think that the PR may be closed? |
Adds a new option to the collection field type. This option allows to reindex the data array if necessary.
TODO: