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

[Form] Do not ignore the choice groups for caching #29695

Merged
merged 1 commit into from Jan 3, 2019

Conversation

Projects
None yet
5 participants
@vudaltsov
Copy link
Contributor

vudaltsov commented Dec 26, 2018

Q A
Branch? 3.4
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

While working on a different issue I suddenly came over a strange behaviour.

$builder
    ->add('choice1', ChoiceType::class, [
        'choices' => [
            'a' => 'a',
            'b' => 'b',
            'c' => 'c',
        ],
        'multiple' => true,
    ])
    ->add('choice2', ChoiceType::class, [
        'choices' => [
            'ab' => [
                'a' => 'a',
                'b' => 'b',
            ],
            'c' => 'c',
        ],
        'multiple' => true,
    ]);

The code above will result in two identical selects:

image

The reason for this is hash generation in Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator::createListFromChoices() — it does not take array structure into account. See the comment and the code below it.

The comment says that the same choice list should be returned for the same collection of choices no matter the structure. This is wrong, because ChoiceListInterface has a method getStructuredValues() and thus a list instance cannot identified by a hash which does not take structure into account.

I propose a simple change that fixes this and allows for similar choice fields with different groupings.

ping @HeahDude

@HeahDude
Copy link
Member

HeahDude left a comment

👍 for me. Thanks!

Ping @stof, I remember discussing this edge case with you in Berlin two years ago (the workaround being the use of group_by option instead of relying on the structured values), it seems it's worth to fix the issue to simplify things in master.

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Jan 3, 2019

@nicolas-grekas nicolas-grekas added the Ready label Jan 3, 2019

@fabpot

fabpot approved these changes Jan 3, 2019

@fabpot

This comment has been minimized.

Copy link
Member

fabpot commented Jan 3, 2019

Thank you @vudaltsov.

@fabpot fabpot merged commit 9007911 into symfony:3.4 Jan 3, 2019

3 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
fabbot.io Your code looks good.
Details

fabpot added a commit that referenced this pull request Jan 3, 2019

bug #29695 [Form] Do not ignore the choice groups for caching (vudalt…
…sov)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] Do not ignore the choice groups for caching

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

While working on a different issue I suddenly came over a strange behaviour.

```php
$builder
    ->add('choice1', ChoiceType::class, [
        'choices' => [
            'a' => 'a',
            'b' => 'b',
            'c' => 'c',
        ],
        'multiple' => true,
    ])
    ->add('choice2', ChoiceType::class, [
        'choices' => [
            'ab' => [
                'a' => 'a',
                'b' => 'b',
            ],
            'c' => 'c',
        ],
        'multiple' => true,
    ]);
```
The code above will result in two identical selects:

![image](https://user-images.githubusercontent.com/2552865/50459865-b3e36980-0980-11e9-8f3d-17f9cfa9a7f8.png)

The reason for this is hash generation in `Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator::createListFromChoices()` — it does not take array structure into account. See [the comment and the code below it](https://github.com/symfony/symfony/blob/7f46dfb1c4ce5e45a5f1918ad6223a3bbdd52a0b/src/Symfony/Component/Form/ChoiceList/Factory/CachingFactoryDecorator.php#L116).

The comment says that the same choice list should be returned for the same collection of choices no matter the structure. This is wrong, because `ChoiceListInterface` has a method `getStructuredValues()` and thus a list instance cannot identified by a hash which does not take structure into account.

I propose a simple change that fixes this and allows for similar choice fields with different groupings.

ping @HeahDude

Commits
-------

9007911 Do not ignore the choice groups for caching

@vudaltsov vudaltsov deleted the vudaltsov:form-choices-caching-factory-flattened branch Jan 3, 2019

This was referenced Jan 6, 2019

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