Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

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!

5
Login or Register to join the conversation
Default user avatar
Default user avatar Abesse Smahi | posted 5 years ago

Hi,
The down link from this page https://knpuniversity.com/s... is serving the wrong file, it serves the ep09 istead of ep10. Please fix it.

Reply

Hi Abesse,

What link do you mean? I don't see any links in the screencast content on this page: https://knpuniversity.com/s... .

Please, clarify it for me and I'll fix it then.

Cheers!

Reply
Default user avatar
Default user avatar Abesse Smahi | Victor | posted 5 years ago

I mean, when clicking the download button in top-right corner instead of downloading the Episode 10 Episode 09 is served from the server. i hope i am clear.

Reply

Ah, now I see... it is the same correct video but with invalid numbering. Nothing important, but we will fix it soon. Thanks for this report!

Cheers!

Reply
Default user avatar
Default user avatar Abesse Smahi | Victor | posted 5 years ago

Thank you very much.

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

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": "^7.1.3",
        "symfony/symfony": "3.4.*", // v3.4.49
        "doctrine/orm": "^2.5", // 2.7.5
        "doctrine/doctrine-bundle": "^1.6", // 1.12.13
        "doctrine/doctrine-cache-bundle": "^1.2", // 1.4.0
        "symfony/swiftmailer-bundle": "^2.3", // v2.6.7
        "symfony/monolog-bundle": "^2.8", // v2.12.1
        "symfony/polyfill-apcu": "^1.0", // v1.23.0
        "sensio/distribution-bundle": "^5.0", // v5.0.25
        "sensio/framework-extra-bundle": "^3.0.2", // v3.0.29
        "incenteev/composer-parameter-handler": "^2.0", // v2.1.4
        "composer/package-versions-deprecated": "^1.11", // 1.11.99.4
        "knplabs/knp-markdown-bundle": "^1.4", // 1.9.0
        "doctrine/doctrine-migrations-bundle": "^1.1", // v1.3.2
        "stof/doctrine-extensions-bundle": "^1.2" // v1.3.0
    },
    "require-dev": {
        "sensio/generator-bundle": "^3.0", // v3.1.7
        "symfony/phpunit-bridge": "^3.0", // v3.4.47
        "nelmio/alice": "^2.1", // v2.3.6
        "doctrine/doctrine-fixtures-bundle": "^2.3" // v2.4.1
    }
}
userVoice