php - Symfony3 - 从 yaml 文件动态下拉

标签 php symfony symfony-forms

我想从 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/

相关文章:

php - CakePHP 2.4 - 数据库更新不保存所有传递的数据一个字段永远不会持久化

java - 无法从 Php Exec() 函数执行 java Jar?

symfony - 如何在 Symfony 3.4 phpunit 测试中访问私有(private)服务?

symfony - 没有应用程序,AppBundle 文件夹

forms - Symfony 表单 EntityType::class - 如何使用它来编辑表单中的数据 (3.2.13)

symfony - 可捕获的 fatal error : Object of class could not be converted to string

php - 繁重的处理导致站点不可用

PHP SwiftMailer Localhost 测试设置

symfony - 如何显示 Victoire 文章的标签?

jquery - 如何在使用 jQuery 隐藏输入字段后正确定位 HTML5 表单验证错误消息?