Symfony3 - 从yaml文件动态下拉

时间:2017-01-31 22:43:42

标签: php symfony symfony-forms

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

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

  

配置/ 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%"

下拉菜单显示option1,option2和option3。

我想看看test1,test2,test3。

我甚至想过像

这样的东西
  

test.yml

test:
 test1:test1
 test2:test2
 test3:test3

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

有没有更好的方法可以做到这一点,还是我覆盖了所有场景?

1 个答案:

答案 0 :(得分:1)

使用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>