Symfony3 - 从 yaml 文件动态下拉

Symfony3 - Dynamic Drop down from a yaml file

我想从 yaml 文件值填充下拉列表

我创建了一个单独的 yaml 文件

config/test.yml

test:
  - test1
  - test2
  - test3

中调用这个

config.yml

imports:
 - { resource: test.yml }

parameters:    
 testing:
      "%test%"

现在使用这个表格

->add('test', ChoiceType::class, array('label' => 'Testing',
                'choices' => $this->getParameter('testing')))

它正在工作,但下拉菜单显示的是索引,即 0、1、2 而不是值。

我试过的其他东西, 试验 1

test.yml

option1: test1
option2: test2
option3: test3

config.yml

testing:
 "%option1%": "%option1%"
 "%option2%": "%option2%"
 "%option3%": "%option3%"

这个东西有用,但我每次添加新选项时都不会更改两个文件

试用 2 刚刚更改了

config.yml

testing:
 "%test%"

下拉菜单显示选项 1、选项 2 和选项 3。

我想看测试 1、测试 2、测试 3。

我什至想到了

test.yml

test:
 test1:test1
 test2:test2
 test3:test3

但在实际情况下,我的 test1 test2 test3 值太大,所以不想让我的 test.yml 看起来难看两次相同的文本

是否有更好的方法来做到这一点,或者我是否涵盖了所有场景?

使用array_flip():

test:
  - test1
  - test2
  - test3

testing:
    "%test%"


'choices' => array_flip($this->getParameter('testing'))

结果:

<option value="0">test1</option>
<option value="1">test2</option>
<option value="2">test3</option>