Error arg 1 $view null given – when displaying a Symfony form in pieces via form_start, form_widget, form_row, etc

huangapple go评论68阅读模式
英文:

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, &#39;use_code_&#39; ~ group.id)) }} I get the following error:

Uncaught PHP Exception TypeError: &quot;Symfony\Component\Form\FormRenderer::searchAndRenderBlock():
Argument #1 ($view) must be of type Symfony\Component\Form\FormView, null given

PHP Controller (Partial Code)

    $form = $this-&gt;createFormBuilder();

    $groups = json_decode($response-&gt;getContent());
    foreach ($groups as $group) {
      $form-&gt;add(&#39;use_code_&#39; . $group-&gt;id, ChoiceType::class, [
        &#39;choices&#39; =&gt; [
          &#39;Select One&#39; =&gt; [
            &#39;none&#39; =&gt; &#39;&#39;,
            &#39;A&#39; =&gt; &#39;A&#39;,
            &#39;B&#39; =&gt; &#39;B&#39;,
            &#39;C&#39; =&gt; &#39;C&#39;,
            &#39;D&#39; =&gt; &#39;D&#39;,
            &#39;E&#39; =&gt; &#39;E&#39;,
            &#39;F&#39; =&gt; &#39;F&#39;,
            &#39;G&#39; =&gt; &#39;G&#39;,
            &#39;H&#39; =&gt; &#39;H&#39;,
            &#39;I&#39; =&gt; &#39;I&#39;,
            &#39;J&#39; =&gt; &#39;J&#39;,
          ],
        ],
      ]);
    }

    $form-&gt;add(&#39;submit&#39;, SubmitType::class, [&#39;label&#39; =&gt; &#39;Submit&#39;]);
    $form = $form-&gt;getForm();

    return $this-&gt;render(&#39;group_mapping_form.html.twig&#39;, [
      &#39;group_mapping_form&#39; =&gt; $form-&gt;createView(),
      &#39;groups&#39; =&gt; $groups ?: &#39;&#39;,
    ]);

Twig File

{% extends &quot;base.html.twig&quot; %}

{% block stylesheets %}
  {{ parent() }}
  &lt;link href=&quot;{{ asset(&#39;css/table.css&#39;) }}&quot; rel=&quot;stylesheet&quot;/&gt;
{% endblock %}

{% block body %}
  {% if group_mapping_form is not empty %}
    {{ form_start(group_mapping_form) }}
    &lt;table class=&quot;styled-table&quot;&gt;
      &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Group Name&lt;/th&gt;
        &lt;th&gt;Entity Use Code&lt;/th&gt;
      &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      {% if groups is not empty %}
        {% for group in groups %}
          &lt;tr&gt;
            &lt;td&gt;
              &lt;div class=&quot;cell-border&quot;&gt;{{ group.name }}&lt;/div&gt;
            &lt;/td&gt;
            &lt;td&gt;
              &lt;div class=&quot;cell-border&quot;&gt;{{ form_widget(attribute(group_mapping_form, &#39;use_code_&#39; ~ group.id)) }}&lt;/div&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
        {% endfor %}
      {% else %}
        &lt;tr&gt;
          &lt;td&gt;No Records Found!&lt;/td&gt;
        &lt;/tr&gt;
      {% endif %}
      &lt;/tbody&gt;
    &lt;/table&gt;

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

huangapple
  • 本文由 发表于 2023年5月26日 00:56:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定