Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Form Gives Catchable Error About FormView

This is really strange, and it happened after updating a Twig template:

The Controller Part

    $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'
            )
    );

The Template Part

{{ 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) }}

The Error

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

As you can clearly see, I am indeed rendering the FormView, and the fact that it's null would lead me to believe that this is related to the RegistrationType() class, but nothing changed there, only in the template to render?

like image 943
JRL Avatar asked Sep 30 '13 20:09

JRL


1 Answers

Alright, this was really specific and I figured it out, so I thought I would update here for anyone that finds this on Google.

The problem was that my original implementation of the form looked like this in twig:

{{ form(form) }}

What this does is go through the whole form variable and display everything that there was to display.

The problem was that my view variables were wrong. This was because the RegistrationType() object added another UserType() object as a user field like this:

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

This meant that the call:

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

Should have been:

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

The main reason I didn't catch this error is because the errors Twig gives didn't tell me which form call it failed on, and I automatically assumed it was the first one, {{ form_start(form) }}. However that call was just fine.

I looked at the displayed HTML and noticed how far it got, and that caused me to investigate the particular line that was the problem.

The Twig template now looks like this and renders correctly:

{{ 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) }}
like image 193
JRL Avatar answered Sep 23 '22 06:09

JRL