我已经完成了Symfony页面上的一些表单教程(特别是How to Embed a Collection of Forms,How To use a Form without a Dataclass和CollectionType Field)。
我正在尝试显示具有多个主要合作伙伴的表单,这些表单可以进行编辑并提交回系统。
但是我收到一条Twig_Runtime_Error:“变量“ lead_partners”不存在”。
我的树枝:
{% block content %}
<div>
{{ form_start(form) }}
{% for partner in lead_partners %}
{{ form_row(partner.name) }}
{% endfor %}
{{ form_end(form) }}
</div>
{% endblock content %}
我的控制器代码:
public function overview(Request $request, \App\Utility\LeadPartnerLoader $LeadPartnerLoader)
{
$leadPartnerList = $LeadPartnerLoader->loadAll();
$form = $this->createFormBuilder($leadPartnerList)
->add('lead_partners', CollectionType::class, [
'entry_type' => LeadPartnerFormType::class,
])->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$data = $form->getData();
}
return $this->render(
'lead_partner_overview2.html.twig',
[
'form' => $form->createView()
]);
}
以及表单类型(LeadPartnerFormType):
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => LeadPartner::class,
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id', HiddenType::class)
->add('name', TextType::class);
}
$leadPartnerList
的类型为array
。
我在做什么错/想念这里吗?
亲切问候
答案 0 :(得分:1)
似乎您的操作overview
不会返回您在模板中使用的lead_partners
变量。
您可以尝试执行此操作
return $this->render(
'lead_partner_overview2.html.twig',
[
'form' => $form->createView(),
'lead_partners' => $leadPartnerList, // I gess that's the list you want to loop ?
]);