Controlling Vars with finishView()

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $10.00

Question: can we control form view variables directly from inside GenusFormType? Of course! Use the "Code"->"Generate" menu, or Command+N on a Mac, click "Override Methods" and then select a method called finishView().

There are actually two methods that are called when your form is transformed into a FormView object: buildView() and finishView(): one is called at the beginning, and the other at the end.

In this case, we want finishView():

... lines 1 - 11
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
... lines 14 - 15
class GenusFormType extends AbstractType
{
... lines 18 - 44
public function finishView(FormView $view, FormInterface $form, array $options)
{
... line 47
}
... lines 49 - 55
}

Don't worry about calling the parent function, it's empty.

The FormView object that's passed to this method is the final, top-level FormView object that represents the entire form. Now, check this out: use $view['funFact']->vars['help'] = and then type a message, like, a nice fun fact suggestion:

... lines 1 - 15
class GenusFormType extends AbstractType
{
... lines 18 - 44
public function finishView(FormView $view, FormInterface $form, array $options)
{
$view['funFact']->vars['help'] = 'For example, Leatherback sea turtles can travel more than 10,000 miles every year!';
}
... lines 49 - 55
}

Congratulations: you've just set that view variable. But let's break it down. The $view variable is the top of our FormView tree. To get a FormView for a specific field, access it like an array key.

At this point, $view['funFact'] gives you the same FormView object that you would get in a template by calling genusForm.funFact. Then, we access the public vars array property and add a help key to it. Ultimately, this adds that view variable.

Refresh to check it out. It works! And now there's nothing we can't change!

But let's do something even harder. Copy the help string, then comment out the finishView() method entirely. Find the funFact field above, pass null as the second option so that Symfony keeps guessing the field type, then add a new help option:

<?php
... lines 2 - 15
class GenusFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
... lines 21 - 29
->add('funFact', null, [
'help' => 'For example, Leatherback sea turtles can travel more than 10,000 miles every year!'
])
... lines 33 - 43
;
}
... lines 46 - 52
}

I want this to ultimately set a help variable for me.

But if you try it now, huge error!

The option "help" does not exist.

That's no surprise: I just invented this option! But, we can make this work.

Leave a comment!

This tutorial is built on Symfony 3 but form theming hasn't changed much in Symfony 4 and Symfony 5. Other than some path differences - this tutorial should work fine.

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.5.9",
        "symfony/symfony": "3.1.*", // v3.1.4
        "doctrine/orm": "^2.5", // v2.7.2
        "doctrine/doctrine-bundle": "^1.6", // 1.6.4
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.3.0
        "symfony/swiftmailer-bundle": "^2.3", // v2.3.11
        "symfony/monolog-bundle": "^2.8", // 2.11.1
        "symfony/polyfill-apcu": "^1.0", // v1.2.0
        "sensio/distribution-bundle": "^5.0", // v5.0.22
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.2
        "knplabs/knp-markdown-bundle": "^1.4", // 1.4.2
        "doctrine/doctrine-migrations-bundle": "^1.1", // 1.1.1
        "stof/doctrine-extensions-bundle": "^1.2" // v1.2.2
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.0.7
        "symfony/phpunit-bridge": "^3.0", // v3.1.3
        "nelmio/alice": "^2.1", // 2.1.4
        "doctrine/doctrine-fixtures-bundle": "^2.3" // 2.3.0
    }
}