如何获取controller中的Config数组参数,发送到twig中以form形式显示
How to get Config array parameters in controller to send it to twig for displaying it in form
我有一个包含位置字段的表单。使用下拉菜单和选项创建 ChoiceType
class 我已将 config.yml 中的所有位置定义为参数。
config.yml:
parameters:
locations:[location1, location2, location3]
控制器表单域:
$form = $this->createFormBuilder($abc)
->add('location', ChoiceType::class, array(
'label' => 'Select Location',
'choices'=> $this->getParameter('locations'),
))
->getForm();
这是显示数组位置,即 0
表示位置 1,1
表示位置 2 等,但我想显示数组值。
我尝试的其他方法,将此参数放在一个数组中,然后尝试 array_value
选项。我必须通过以下方式显示位置:
$arrayincontroller=$this->getParameter('locations')
形式为:
->add('location', ChoiceType:class, array('label' => 'Select Location', 'choices'=> array($arrayincontroller[0] => $arrayincontroller[0])
但不想像这样写每个位置。有没有更好的方法将值传递给 form.试图包括 foreach 但我猜我做不到。
您可以将参数定义为数组,如下所示:
parameters:
locations:
location1: location1
location2: location2
location3: location3
始终检索为:
$arrayincontroller=$this->getParameter('locations')
但使用如下:
->add('location', ChoiceType:class, array('label' => 'Select Location', 'choices'=> $arrayincontroller);
希望对您有所帮助
我有一个包含位置字段的表单。使用下拉菜单和选项创建 ChoiceType
class 我已将 config.yml 中的所有位置定义为参数。
config.yml:
parameters:
locations:[location1, location2, location3]
控制器表单域:
$form = $this->createFormBuilder($abc)
->add('location', ChoiceType::class, array(
'label' => 'Select Location',
'choices'=> $this->getParameter('locations'),
))
->getForm();
这是显示数组位置,即 0
表示位置 1,1
表示位置 2 等,但我想显示数组值。
我尝试的其他方法,将此参数放在一个数组中,然后尝试 array_value
选项。我必须通过以下方式显示位置:
$arrayincontroller=$this->getParameter('locations')
形式为:
->add('location', ChoiceType:class, array('label' => 'Select Location', 'choices'=> array($arrayincontroller[0] => $arrayincontroller[0])
但不想像这样写每个位置。有没有更好的方法将值传递给 form.试图包括 foreach 但我猜我做不到。
您可以将参数定义为数组,如下所示:
parameters:
locations:
location1: location1
location2: location2
location3: location3
始终检索为:
$arrayincontroller=$this->getParameter('locations')
但使用如下:
->add('location', ChoiceType:class, array('label' => 'Select Location', 'choices'=> $arrayincontroller);
希望对您有所帮助