我想用数组填充我的ChoiceType,但它看起来像是用ID填充而不是用值填充。表单被正确显示,但选项是'0','1'...而不是数组中的名称。Symfony用数组填充ChoiceType
这是我的控制器:
$categories = $this->getDoctrine()->getRepository('myBundle:Category')->findAll();
$techChoices = array();
$i = 0;
foreach($categories as $t) {
$techChoices[$i] = $t->getName();
$i = $i + 1;
}
$formOptions = array('categories' => $techChoices);
$document = new Document($categories);
$form = $this->createForm(DocumentType::class, $document, $formOptions);
这是我buildForm:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', FileType::class)
->add('file_name', TextType::class)
->add('file_description', TextType::class)
->add('file_group', ChoiceType::class, array(
'choices' => $options['categories'],
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'categories' => array(),
));
}
非常感谢,第二个链接正是我所需要的。它现在有效 – LordArt