我想从 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>
关于php - Symfony3 - 从 yaml 文件动态下拉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41968865/