Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[2.7] Add documentation for choice fields with duplicate labels #5905

Closed
Labels
@webmozart

Description

@webmozart

With 2.7, the way of creating choice fields with duplicate labels has changed. When before (withchoices_as_values set tofalse) it was possible to have choices in different choice groups that shared the same labels, this is not possible anymore:

$builder->add('country','choice', ['choices' => ['Top' => ['FR' =>'France','DE' =>'Germany',        ],'All' => ['AT' =>'Austria',// ...'DE' =>'Germany',// ...        ],    ],]);

This won't work. The reason is that thechoice keys (i.e. the LHS of the array) must be unique. You can solve this use case by settingchoice_label to a closure. This closure accepts the choice, the choice key and the choice value as arguments. You can convert the unique keys to non-unique labels in there:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp