EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

implements array flattening

Open meiyasan opened this issue 5 years ago • 11 comments

array_flip() function returns a "Warning: array_flip(): Can only flip STRING and INTEGER values!" when a nested associative array is passed as argument. In case of multiple choices selected entries are not retrieved from form.

I flattened the array before flipping to get them e.g: array("key1" => array("A" => "a", "B" => "b")) becomes array("A" => "a", "B" => "b") and then array("a" => "A", "b" => "B") by flipping

meiyasan avatar Jan 03 '21 21:01 meiyasan

can someone advice ? thanks.

meiyasan avatar Jan 03 '21 22:01 meiyasan

Could you please add a testcase?

OskarStark avatar Jan 04 '21 07:01 OskarStark

I don't understand the need for this feature. Yes, you need to pass a "flat array" to form choices ... but why would you pass a nested array to it? It's like in a Symfony app, right? You must pass a "flat array" yourself ... Symfony doesn't convert it for you.

Also, flattening an array is not only "non trivial" ... but it's also highly dependent on the application. E.g. in case of duplicated keys when flattening, should we keep the first occurrence or the last one?

I'd say we don't need to implement this feature and each app is responsible of generating the right array for the form choices. If I'm wrong, please explain it to me. Thanks!

javiereguiluz avatar Jan 07 '21 19:01 javiereguiluz

Common, this is not only a feature. This is first an error when passing a complex array. ChoiceType obviously accepts nested array to display in an proper way "select" tags with sections, while EA not.

Providing nested array gives the opportunity to show a form in a simple way as following: Capture d’écran 2021-01-07 à 21 00 22

(e.g. On the picture you get section "Génériques" then its entries, and a section "Officiels" and its entries. Only entries can be selected)

Generally speaking, flattening an array is maybe not trivial, but flattening here is just a way to retrieve the selected entries when showing back a form with choices.

Also, flattening an array is not only "non trivial" ... but it's also highly dependent on the application. E.g. in case of duplicated keys when flattening, should we keep the first occurrence or the last one?

The purpose of flattening the array is here only to retrieve the value associated to a displayed label displayed in the form.
Your question does not make sense, @javiereguiluz.. These choices are unique, it is a direct match between what is shown and the value of "select" entry.

Moreover, this issue also applies without nested array.. You get "User1" and "User2" labels, their values are (let's say) "user1", "user2". It does not make sense to build a form with User1 and User2 label pointing to the same value. This would just be a confusion of the developer.

meiyasan avatar Jan 07 '21 20:01 meiyasan

https://symfony.com/doc/current/reference/forms/types/choice.html#grouping-options

meiyasan avatar Jan 07 '21 22:01 meiyasan

I flattened the array before flipping to get them e.g: array("key1" => array("A" => "a", "B" => "b")) becomes array("A" => "a", "B" => "b") and then array("a" => "A", "b" => "B") by flipping

I think the result should be array("key1" => array("a" => "A", "b" => "B")) We should keep support for Grouping Options if the choices are grouped.

Seb33300 avatar Jan 19 '21 08:01 Seb33300

The only purpose of this function is to be able to retrieve selected element. It doesn't modify the class attributes. So it doesn't really matter. I think you just make the reading of the array more complicate going that way.

meiyasan avatar Jan 19 '21 13:01 meiyasan

I've tested this bug in one of my apps. I use the same choices as your test:

->setChoices([
    'foo'  => ['A' => 'a', 'B' => 'b'],
    'bar'  => ['C' => 'c', 'D' => 'd'],
    'john' => 'doe'
]);

The result, without applying the changes of this PR, is the following:

image

So, it works as expected and I don't see any error in the "array flipping" process. What am I missing here?

javiereguiluz avatar Apr 25 '21 11:04 javiereguiluz

Did you enable multiple choices ?

meiyasan avatar May 09 '21 10:05 meiyasan

I overlooked your message @javiereguiluz. Please just dump() something in src/Field/Configurator/ChoiceConfigurator.php around the array_flip function and share the full lines around "->setChoices()".

NB: Just run the test case with and without applying the changes

meiyasan avatar May 10 '21 22:05 meiyasan

easycorp/easyadmin-bundle 3.5.1 still have this problem when using group options:

Warning: array_flip(): Can only flip STRING and INTEGER values!

tourze avatar Mar 18 '22 05:03 tourze

Replaced by the fix implemented in #5374 and which will be available in the next stable release. Thanks.

javiereguiluz avatar Sep 20 '22 18:09 javiereguiluz