Skip to content
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] Improve the group_by option for choices #30315

Open
javiereguiluz opened this Issue Feb 20, 2019 · 9 comments

Comments

Projects
None yet
3 participants
@javiereguiluz
Copy link
Member

javiereguiluz commented Feb 20, 2019

Description
In Symfony 2.7 (with choices_as_values set to false) it was possible to have choices in different choice groups that shared the same labels, this is not possible anymore.

Instead of this hack proposed in Symfony Docs, @Tobion wondered in this comment if this use-case be better solved by allowing group_by option to return an array of groups? This way people would just add the choices once and then could add them to multiple groups if needed.

@antonch1989

This comment has been minimized.

Copy link
Contributor

antonch1989 commented Feb 20, 2019

@javiereguiluz I could try to handle this one

@javiereguiluz

This comment has been minimized.

Copy link
Member Author

javiereguiluz commented Feb 20, 2019

@antonch1989 great! Thank you.

If you estimate that this task will be very complex or if you think multiple different implementations are possible, you can ask here for advice to people like @vudaltsov @HeahDude and @xabbuh who are experts in Symfony Forms and are willing to help the community.

@antonch1989

This comment has been minimized.

Copy link
Contributor

antonch1989 commented Feb 21, 2019

it-works
The issue was create a long time ago. Does this problem still exist? I ask because I don't see it with "symfony/form": "4.2.*" in my composer.json

@xabbuh

This comment has been minimized.

Copy link
Member

xabbuh commented Feb 21, 2019

@antonch1989 Can you share the configuration of your form type?

@antonch1989

This comment has been minimized.

Copy link
Contributor

antonch1989 commented Feb 21, 2019

`
$builder = $this->createFormBuilder();

    $builder->add('country', ChoiceType::class, [
        'choices' => [
            'Top' => [
                'FR' => 'France',
                'DE' => 'Germany',
            ],
            'All' => [
                'AT' => 'Austria',
                // ...
                'DE' => 'Germany',
                // ...
            ],
        ],
    ])
        ->add('submit', SubmitType::class)
    ;`
@xabbuh

This comment has been minimized.

Copy link
Member

xabbuh commented Feb 21, 2019

But you are not using the group_by option here at all. :)

@antonch1989

This comment has been minimized.

Copy link
Contributor

antonch1989 commented Feb 21, 2019

@xabbuh sorry, I don't get it :) In the documentation diff created by @webmozart was written that it doesn't work with this configuration. That's why the hack with choice_label was suggested. Afterwards a proper solution with adding more functionality to group_by was proposed by @Tobion. But if it works then why we need this? Could you clarify a bit, probably I missunderstood the situation.

@xabbuh

This comment has been minimized.

Copy link
Member

xabbuh commented Feb 25, 2019

@antonch1989 If I understand the request correctly, the goal would be that instead of

$builder->add('country', 'choice', [
    'choices' => [
        'Top' => [
            'Top_France' => 'FR',
            'Top_Germany' => 'DE',
        ],
        'All' => [
            'Austria' => 'AT',
            // ...
            'Germany' => 'DE',
            // ...
        ];,
    ],
    'choice_label' => function ($choice, $key) {
        if ('Top_' === substr($key, 0, 4)) {
            // Strip "Top_"
            return substr($key, 4);
        }

        // Return unchanged
        return $key;
    },
]);

you could use this:

$builder->add('country', 'choice', [
    'choices' => [
        'Austria' => 'AT',
        'France' => 'FR',
        'Germany' => 'DE',
        // ...
    ],
    'group_by' => function ($choice) {
        if (in_array($choice, ['DE', 'FR'], true)) {
            return ['Top', 'All'];
        }

        return ['All'];
    },
]);
@antonch1989

This comment has been minimized.

Copy link
Contributor

antonch1989 commented Feb 27, 2019

oh, I see. I'll try again in this case, thanks for clarifying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.