5

这真的很奇怪,它发生在更新 Twig 模板之后:

控制器部分

    $registration = new Registration();
    $form = $this->createForm(new RegistrationType(), $registration, array(
        'action' => $this->generateUrl('account_create'),
    ));

    return $this->render(
        'AcmeCommonBundle:Security:register.html.twig',
        array(
            'form' => $form->createView(),
            'contentBackColor' => 'canvasGreen'
            )
    );

模板部分

{{ form_start(form) }}
    {{ form_errors(form) }}

    <p>All Fields Are Required</p>

    <div class="form-row">
        {{ form_label(form.firstname, "First Name") }}
        {{ form_widget(form.firstname) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.firstname) }}
    </div>

    <div class="form-row">
        {{ form_label(form.lastname, "Last Name") }}
        {{ form_widget(form.lastname) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.lastname) }}
    </div>

    <div class="form-row">
        {{ form_label(form.username, "Username") }}
        {{ form_widget(form.username) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.username) }}
    </div>

    <div class="form-row">
        {{ form_label(form.email, "Email") }}
        {{ form_widget(form.email) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.email) }}
    </div>

    <div class="form-row">
        {{ form_label(form.password, "Password") }}
        {{ form_widget(form.password) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.password) }}
    </div>

    <div class="form-row">
        {{ form_label(form.confirm, "Confirm Password") }}
        {{ form_widget(form.confirm) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.confirm) }}
    </div>

    <div class="form-row">
        {{ form_label(form.terms, "Terms & Conditions") }}
        {{ form_widget(form.terms) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.terms) }}
    </div>

{{ form_end(form) }}

错误

Argument 1 passed to Symfony\Component\Form\FormRenderer::searchAndRenderBlock() must be an instance of Symfony\Component\Form\FormView, null given

如您所见,我确实在渲染 FormView,它为 null 的事实让我相信这与 RegistrationType() 类有关,但那里没有任何变化,只是在要渲染的模板中?

4

1 回答 1

5

好吧,这真的很具体,我想通了,所以我想我会在这里为任何在 Google 上找到它的人更新。

问题是我最初的表单实现在树枝中看起来像这样:

{{ form(form) }}

这样做是遍历整个form变量并显示要显示的所有内容。

问题是我的视图变量是错误的。这是因为该RegistrationType()对象添加了另一个UserType()对象作为user字段,如下所示:

->add('user', new UserType())

这意味着调用:

{{ form_label(form.firstname, "First Name") }}

本来应该:

{{ form_label(form.user.firstname, "First Name") }}

我没有发现这个错误的主要原因是因为 Twig 给出的错误没有告诉我它在哪个表单调用上失败了,我自动假设它是第一个,{{ form_start(form) }}. 然而,那个电话很好。

我查看了显示的 HTML 并注意到它有多远,这使我调查了问题所在的特定行。

Twig 模板现在看起来像这样并正确呈现:

{{ form_start(form) }}
    {{ form_errors(form) }}

    <p>All Fields Are Required</p>

    <div class="form-row">
        {{ form_label(form.user.firstname, "First Name") }}
        {{ form_widget(form.user.firstname) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.user.firstname) }}
    </div>

    <div class="form-row">
        {{ form_label(form.user.lastname, "Last Name") }}
        {{ form_widget(form.user.lastname) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.user.lastname) }}
    </div>

    <div class="form-row">
        {{ form_label(form.user.username, "Username") }}
        {{ form_widget(form.user.username) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.user.username) }}
    </div>

    <div class="form-row">
        {{ form_label(form.user.email, "Email") }}
        {{ form_widget(form.user.email) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.user.email) }}
    </div>

    <div class="form-row">
        {{ form_label(form.user.password.password, "Password") }}
        {{ form_widget(form.user.password.password) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.user.password.password) }}
    </div>

    <div class="form-row">
        {{ form_label(form.user.password.confirm, "Confirm Password") }}
        {{ form_widget(form.user.password.confirm) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.user.password.confirm) }}
    </div>

    <div class="form-row">
        {{ form_label(form.terms, "Terms & Conditions") }}
        {{ form_widget(form.terms) }}
        <div class="clear-fix"></div>
    </div>
    <div class="form-errors-row">
        {{ form_errors(form.terms) }}
    </div>

{{ form_end(form) }}
于 2013-09-30T21:31:03.913 回答