Chapters
41 Chapters
|
4:45:40
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
21.
Form Theming & Variables
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
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.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.2
"doctrine/doctrine-bundle": "^1.6.10", // 1.10.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.0
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-time-bundle": "^1.8", // 1.8.0
"nexylan/slack-bundle": "^2.0,<2.2.0", // v2.0.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.0
"sensio/framework-extra-bundle": "^5.1", // v5.2.1
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.1.6
"symfony/cache": "^3.3|^4.0", // v4.1.6
"symfony/console": "^4.0", // v4.1.6
"symfony/flex": "^1.0", // v1.21.6
"symfony/form": "^4.0", // v4.1.6
"symfony/framework-bundle": "^4.0", // v4.1.6
"symfony/property-access": "^3.3|^4.0", // v4.1.6
"symfony/property-info": "^3.3|^4.0", // v4.1.6
"symfony/security-bundle": "^4.0", // v4.1.6
"symfony/serializer": "^3.3|^4.0", // v4.1.6
"symfony/twig-bundle": "^4.0", // v4.1.6
"symfony/validator": "^4.0", // v4.1.6
"symfony/web-server-bundle": "^4.0", // v4.1.6
"symfony/yaml": "^4.0", // v4.1.6
"twig/extensions": "^1.5" // v1.5.2
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.0.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/debug-bundle": "^3.3|^4.0", // v4.1.6
"symfony/dotenv": "^4.0", // v4.1.6
"symfony/maker-bundle": "^1.0", // v1.8.0
"symfony/monolog-bundle": "^3.0", // v3.3.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.1.6
"symfony/stopwatch": "^3.3|^4.0", // v4.1.6
"symfony/var-dumper": "^3.3|^4.0", // v4.1.6
"symfony/web-profiler-bundle": "^3.3|^4.0" // v4.1.6
}
}
19 Comments
Hey :)
At 8:00 i'm struggling to understand how the placeholder attr is passed to the form_widget call in our custom form theme block, as the only array of variables that is passed is widget_attr and it seems to be empty...
Any clarification?
Thanks for your time!
Does form rendering functions variables "injected" in the form theme block means they are also accessible in any subsequent function scope without specifically passing them during the function call?
I realize that my understanding of twig variables is limited and that's why i can't seem to understand...
Hi,
That is not directly twig variables, but how the form system works. When it renders, it has access to the form variable and it has a bunch of values inside. You can try to print
{% dump %}inside a theme block and you will see everything that was passed inside.Cheers!
Hi
I need to customise the aspect of some items of a collection depending on values I have in a custom variable sent by the controller to the form view. Is there a way to pass a custom variable from the "parent" template to a form theme ? Thanks!
Hey Cyril S.!
Sorry for the slow reply! I would do that by passing a "var" into form_widget:
That's it! As you noticed, the main template and the form theme template and totally isolated. But anything you pass as the 2nd argument of form_widget (or form_row) becomes available as a local variable in the form theme template.
Cheers!
Mmm... I tried that previously but it seems not working in my case :-(
I oversimplified my issue to ask my question and, you're right, it works well in that simple case.
But, in fact, I have 4 files and 2 includes in my form:
file1 - main template (here defining theme path) > file2 - include > file 3 - include (here form_widget)
file4 - form theme template
When defining the form theme template in the included template, the theme doesn't apply and when defining the form theme template in the main template, the variable isn't transfered to the form theme template...
Hope my explanations are clear :-\
Cyril
Hey Cyril S.!
Hmm. I'll admit that I can't remember how defining form themes works when you have included templates. I can say this, however: assuming you *are* able to get the correct form theme to be called for your form, passing a variable (via the 2nd argument of form_widget()) *will* be passed to the form theme template. That's a very basic mechanism to how the form theme system works. The logic is this:
A) Symfony determines which form theme to use (and, as you know, it has a few mechanisms to do that, like global form themes and per-template form themes. This is the part where I can't remember how it works when you use include()).
B) Symfony calls the blocks it needs from the form theme template. It exposes all of the field "vars" as local variables to that block. The vars are a combination of "vars" that were set in the form class for that field (e.g. via buildView() or finishView()) + whatever you pass as the 2nd argument into form_widget().
So, I'm can't say why it's not working... except to focus first on seeing if you can get the correct form theme to be executed. Then, the vars just "should work"... hopefully ;).
Cheers!
I want to make a form with an undefiened amount of input fields. For example: TODO List A, might have 3 items (input fields). TODO List B, might have 5 items.
What is the best approach? Is there a tutorial / chapter that explains this?
My think process right now is this (but I might have the wrong think process): WIth Javscript I try to make a onClick that appends new input fields / rows on the form. Then it somehow needs to get connected with the right amount of persist() methods. It then uses flush() on form submit.
But currently I have not started coding this, because I am thinking wether there is a 'official Symfony way' of doing this?
Hey Farry,
I think what you need is the ability to add more fields of a specific type into your form.
To get a better idea you can read the docs here: https://symfony.com/doc/cur...
or watch this video: https://symfonycasts.com/sc...
Cheers!
I have a general question. We usually try to minimize the data by for example, compressing our css and javascript files. But on the other side, Symfony uses a giant dump for each row rendered (like you mentioned). Doesn't that make compressing our javascript and css almost obsolete? It feels like the amount of data that is being used in the background is a lot. It makes me wonder how it impacts the performance compared to compressing js and css?
Was it only meant as showcase to use the attr directly in the template in this particular case or is there a resaon why not just using 'attr' => ['placeholder' => 'Test'], in the UserRegistrationFormType for those two fields?
Hey @Ralf,
It depends on situation, there is a possibility that you will need to reuse form but with different placeholder or label. In this case it will be more practical to put it in template, also important case is that it's a page styling option and it will be better to put in template so if someone will need to adjust styling or test will not search the form class. Probably it may be person who know nothing about symfony structure and how to guess where it can be.
Cheers!
I am using different form themes for admin panel and front pages. Is there an easy way to define it seperately? When I try to define in base.html.twig, it gives "Variable "form" does not exist." runtime error if there is no form in page. Do I must to define in all twig files that has forms like that:
{% form_theme form with ['form_themes/admin.html.twig'] %}I've solved:
`{% if form is defined %}
{% endif %}`
Hey @Caglar
You can have a default form_theme for you entire page and only change it where you need it by defining the form_theme to use inside the template
Cheers!
Typo error into code block : {{ form_row(registrationForm.agreedTermsAt) }}
Hey Stéphane,
Thank you for this report, but I think you're wrong - it should be "{{ form_row(registrationForm.agreeTerms) }}". If you look at UserRegistrationFormType - you will see exactly "agreeTerms" field that is not mapped, i.e. "mapped = false" that means it's a virtual field in form that is not mapped to User entity at all. We can't use agreedTermsAt because it's not a boolean field but a Datetime object.
Cheers!
Hey Victor,
You are right. I have written 'agreedTermsAt' into buildForm fonction of UserRegistrationFormType class.
Sorry for that.
No problem!
Glad it was not on our side, easy win for us :)
Cheers!
"Houston: no signs of life"
Start the conversation!