使用 FormBuilder 时如何修复 'Variable does not exist'?

How to fix 'Variable does not exist' when using FormBuilder?

我已经完成了 Symfony 页面上的一些表单教程(尤其是 How to Embed a Collection of Forms, How To use a Form without a Dataclass & CollectionType Field)。

我正在尝试显示一个包含多个主要合作伙伴的表单,该表单可以被编辑并提交回系统。

但我得到一个 Twig_Runtime_Error 说法:''Variable "lead_partners" does not exist''。

我的树枝:

{% 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.

类型

我在wrong/missing这里做什么?

亲切的问候

您的操作 overview 似乎没有 return 您在模板中使用的 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 ?
    ]);