Skip to content

Form with an EntityType field with multiple=true giving back array and ArrayCollection #23927

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

Closed
stoccc opened this issue Aug 18, 2017 · 10 comments

Comments

@stoccc
Copy link
Contributor

stoccc commented Aug 18, 2017

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 2.8.22

I have a strange behaviour with a form (mapped to an array, not to a Entity) containing a field of type EntityType with multiple=true.

If I set that field with initial data an empty array, e.g. array(), after successful submit when I call $form->getData() I get an ArrayCollection for that field.

Instead, if I set that field with initial data a non empty array, after a successful submit when I call $form->getData() I get a simple array for that field.

Shouldn't I obtain always the same type?

@linaori
Copy link
Contributor

linaori commented Aug 18, 2017

I don't understand it completely, but it seems like you're using an EntityType while you should be using a collection type of sorts?

@stoccc
Copy link
Contributor Author

stoccc commented Aug 18, 2017

I added two tests here #23930

@yceruto
Copy link
Member

yceruto commented Aug 18, 2017

AFAIK the initial & expected data for EntityType with multiple = true should be an ArrayCollection instance.

Even though, an array is allowed, it shouldn't be recommended right now, because the core MergeCollectionListener doesn't care about the data type of the normalized data:

$dataToMergeInto = $event->getForm()->getNormData();
$data = $event->getData();

Hence $event->getForm()->getNormData() and $event->getData() won't have the same data type and the ArrayCollection type is loses if non "empty" data is provided by default.

Later, you'll needs take care about ambiguous data type resultant.

@yceruto
Copy link
Member

yceruto commented Aug 18, 2017

However, I think passing an array should be allowed too and the data type resultant should be consistent with the default data type. I can provide a solution for it, if it is what we want.

This is where that's not true - when an empty array is provided as default data:

if (!$dataToMergeInto) {
// No original data was set. Set it if allowed
if ($this->allowAdd) {
$dataToMergeInto = $data;
}

then the result will be an empty ArrayCollection() instead of array().

Status: Confirmed

@yceruto
Copy link
Member

yceruto commented Aug 23, 2017

The solution to be consistent with default data type provided:

- if (!$dataToMergeInto) {
+ if (null === $dataToMergeInto) {

and the resulting data type should be consistent:

  • If an array was given => an array is retrieved.
  • If an ArrayCollection was given => an ArrayCollection is retrieved.

@stoccc could you try it?

@stoccc
Copy link
Contributor Author

stoccc commented Aug 24, 2017

@yceruto OK, it works. If I give an array, I always get an array; if I give an ArrayCollection, I always get an ArrayCollection

@yceruto
Copy link
Member

yceruto commented Aug 24, 2017

@stoccc If you prefer, you can provide the fix and update your PR, or let me know to submit a new one :)

@stoccc
Copy link
Contributor Author

stoccc commented Aug 24, 2017

@yceruto ok, I committed your changes

@stoccc
Copy link
Contributor Author

stoccc commented Aug 25, 2017

@yceruto I created new PR #23980 against 2.7. It seems that in php 5.3 tests pass, while in php 7 and 7.1 they don't pass. I can't figure out why. In my installation with 2.8 on php 7.0.10 I don't have problems. :(

@pejaycz
Copy link

pejaycz commented Sep 14, 2017

I have same problem with Symfony 3.3.6 (PHP 5.6)

fabpot added a commit that referenced this issue Oct 1, 2017
…field with multiple=true (stoccc)

This PR was squashed before being merged into the 2.7 branch (closes #23980).

Discussion
----------

Tests and fix for issue in array model data in EntityType field with multiple=true

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| License       | MIT
| Fixed tickets | #23927

Provided some tests and the fix for #23927.
Rebased to 2.7, replaces #23930

Commits
-------

aaba6b4 Tests and fix for issue in array model data in EntityType field with multiple=true
@fabpot fabpot closed this as completed Oct 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants