无法在 symfony 2 twig 文件中设置选择

Unable to set selected in symfony 2 twig file

我有一个表单,其中下拉值来自另一个实体。

->add('country', 'entity', array(
                'label' => 'Country',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('u')
                        ->orderBy('u.countryName', 'ASC');
                },
                'empty_value' => 'Select Country',
                'required' => true,
                'mapped' => true,
                'class' => 'BundleAdminBundle:KidsKulaCountry',
                'attr' => array('class' => 'form-control sml-frm'),
                //...
            ))

我想在 twig 文件中设置编辑时选择的值。

{{ form_widget(form.country, {value: Setcountry } ) }}

如果我打印

{{Setcountry}}

它的 returns 结果,但它没有设置 selected.Please 帮助我。

我假设您的 Setcountry 是您想要 select 的实体的 id。您可以通过将 int 的 Setcountry 转换为 string 下一个方式:

{{ form_widget(form.country, {value: Setcountry ~ "" } ) }}