英文:
Error arg 1 $view null given - when displaying a Symfony form in pieces via form_start, form_widget, form_row, etc
问题
当我使用 {{ form(group_mapping_form) }}
时,表单会渲染,但当我尝试使用 {{ form_widget(attribute(group_mapping_form, 'use_code_' ~ group.id)) }}
访问动态生成的表单字段名称时,我会收到以下错误消息:
未捕获的 PHP 异常 TypeError: "Symfony\Component\Form\FormRenderer::searchAndRenderBlock(): 参数 #1 ($view) 的类型必须是 Symfony\Component\Form\FormView,但给定的是 null"
PHP 控制器(部分代码)
$form = $this->createFormBuilder();
$groups = json_decode($response->getContent());
foreach ($groups as $group) {
$form->add('use_code_' . $group->id, ChoiceType::class, [
'choices' => [
'Select One' => [
'none' => '',
'A' => 'A',
'B' => 'B',
'C' => 'C',
'D' => 'D',
'E' => 'E',
'F' => 'F',
'G' => 'G',
'H' => 'H',
'I' => 'I',
'J' => 'J',
],
],
]);
}
$form->add('submit', SubmitType::class, ['label' => 'Submit']);
$form = $form->getForm();
return $this->render('group_mapping_form.html.twig', [
'group_mapping_form' => $form->createView(),
'groups' => $groups ?: '',
]);
Twig 文件
{% extends "base.html.twig" %}
{% block stylesheets %}
{{ parent() }}
<link href="{{ asset('css/table.css') }}" rel="stylesheet"/>
{% endblock %}
{% block body %}
{% if group_mapping_form is not empty %}
{{ form_start(group_mapping_form) }}
<table class="styled-table">
<thead>
<tr>
<th>Group Name</th>
<th>Entity Use Code</th>
</tr>
</thead>
<tbody>
{% if groups is not empty %}
{% for group in groups %}
<tr>
<td>
<div class="cell-border">{{ group.name }}</div>
</td>
<td>
<div class="cell-border">{{ form_widget(attribute(group_mapping_form, 'use_code_' ~ group.id)) }}</div>
</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td>No Records Found!</td>
</tr>
{% endif %}
</tbody>
</table>
{{ form_widget(form.submit) }}
{{ form_end(group_mapping_form) }}
{% endif %}
{% endblock %}
这是您要翻译的内容。
英文:
When I use {{ form(group_mapping_form) }}
the form renders, but when I try to access dynamically generated form field names with {{ form_widget(attribute(group_mapping_form, 'use_code_' ~ group.id)) }}
I get the following error:
Uncaught PHP Exception TypeError: "Symfony\Component\Form\FormRenderer::searchAndRenderBlock():
Argument #1 ($view) must be of type Symfony\Component\Form\FormView, null given
PHP Controller (Partial Code)
$form = $this->createFormBuilder();
$groups = json_decode($response->getContent());
foreach ($groups as $group) {
$form->add('use_code_' . $group->id, ChoiceType::class, [
'choices' => [
'Select One' => [
'none' => '',
'A' => 'A',
'B' => 'B',
'C' => 'C',
'D' => 'D',
'E' => 'E',
'F' => 'F',
'G' => 'G',
'H' => 'H',
'I' => 'I',
'J' => 'J',
],
],
]);
}
$form->add('submit', SubmitType::class, ['label' => 'Submit']);
$form = $form->getForm();
return $this->render('group_mapping_form.html.twig', [
'group_mapping_form' => $form->createView(),
'groups' => $groups ?: '',
]);
Twig File
{% extends "base.html.twig" %}
{% block stylesheets %}
{{ parent() }}
<link href="{{ asset('css/table.css') }}" rel="stylesheet"/>
{% endblock %}
{% block body %}
{% if group_mapping_form is not empty %}
{{ form_start(group_mapping_form) }}
<table class="styled-table">
<thead>
<tr>
<th>Group Name</th>
<th>Entity Use Code</th>
</tr>
</thead>
<tbody>
{% if groups is not empty %}
{% for group in groups %}
<tr>
<td>
<div class="cell-border">{{ group.name }}</div>
</td>
<td>
<div class="cell-border">{{ form_widget(attribute(group_mapping_form, 'use_code_' ~ group.id)) }}</div>
</td>
</tr>
{% endfor %}
{% else %}
<tr>
<td>No Records Found!</td>
</tr>
{% endif %}
</tbody>
</table>
{{ form_widget(form.submit) }}
{{ form_end(group_mapping_form) }}
{% endif %}
{% endblock %}
答案1
得分: -1
我只需要将 {{ form_widget(form.submit) }}
更改为 {{ form_widget(group_mapping_form.submit) }}
,问题就解决了。
英文:
I just needed to change {{ form_widget(form.submit) }}
to {{ form_widget(group_mapping_form.submit) }}
and that fixed it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论