Buy

Controlling Vars with finishView()

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():

57 lines src/AppBundle/Form/GenusFormType.php
... 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:

57 lines src/AppBundle/Form/GenusFormType.php
... 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:

54 lines src/AppBundle/Form/GenusFormType.php
<?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!

  • 2016-12-23 Abesse Smahi

    Thank you very much.

  • 2016-12-23 Victor Bocharsky

    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!

  • 2016-12-23 Abesse Smahi

    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.

  • 2016-12-23 Victor Bocharsky

    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!

  • 2016-12-22 Abesse Smahi

    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.