Chapters
-
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!
Form Type Class
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Hey friends! And welcome to, what I think will be, a super fun tutorial: the one about cookies! Um, forms!
The first question you might ask is: forms? Do we even need forms anymore in this age of JavaScript frontends? Aren't forms so 2016? The answer is... it depends. I get to talk to a lot of developers and, honestly, it comes down to what you're building. Yea, some apps are using modern JavaScript frontends. But just as many are building form-rich interfaces. So, if that's you - hi! o/.
There is no more powerful form system on the planet than Symfony's Form component. Oh, and there are so many pieces to a form: rendering the form, handling the submit, validating data, normalizing data, and other things that you don't even think about, like CSRF protection. Here's the truth about Symfony's Form component: yes, it is crazy powerful. And when you learn to harness that power, you will be incredibly productive. At the same time, in some situations, the form system can be really hard & complex. It can make your job harder than if you didn't use it at all!
So here is our big goal: to learn how to do almost everything you can think of with a form and to identify those complex scenarios, and find the simplest path through them. After all, even if you use and love the form system, it doesn't mean that you have to use it in every single situation.
Project Setup
As always, to become the master of form tags, inputs & textareas, you should totally code along with me. Download the course code from this page. When you unzip it, you'll find a start/
directory inside with the same files that you see here. Open up the README.md
file for instructions on how to get the site set up. The last step will be to find a terminal, move into the project, sip some coffee, and run:
php bin/console server:run
to start the built-in web server. Woo! Now, find your browser and head to http://localhost:8000
. Welcome to our work-in-progress masterpiece: The Space Bar! Our intergalactic news site where aliens everywhere can quickly catch up on only the most important news... after a 500 year nap in cryosleep.
Thanks to our last tutorial, we can even log in! Use admin2@thespacebar.com
, password engage
. Then head over to /admin/article/new
to see.... oh! A big TODO!
Yep! We can display articles but... we can't actually create or edit them yet. The code behind this lives in src/Controller/ArticleAdminController.php
and, sure enough, past us got lazy and just left a TODO.
Creating a Form Class
Time to get to work! The first step to building a form is always to create a form class. Inside src
, add a new Form/
directory... though, like normal, you can put this stuff wherever you want. Inside, a new PHP class called ArticleFormType
. Form classes are usually called form "types", and the only rule is that they must extend a class called AbstractType
. Oh! But of course! I can't find that class because... we haven't installed the form system yet! No problem!
Find your terminal, open a new tab, have another well-deserved sip of coffee, and run:
composer require form
Perfect! Back in our editor, once PhpStorm finishes indexing, we should be able to find the AbstractType
class from the Form
component.
Got it! Now, go to the Code -> generate menu, or Cmd+N on a Mac, and click override methods. There are several methods that you can override to control different parts of your form. But, by far, the most important is buildForm()
.
Show Lines
|
// ... lines 1 - 4 |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
class ArticleFormType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
} | |
} |
Inside this method, our job is pretty simple: use this $builder
object to, um... build the form! Use $builder->add()
to add two fields right now: title
and content
. These are the two most important fields inside the Article
entity class.
Show Lines
|
// ... lines 1 - 9 |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('title') | |
->add('content') | |
; | |
} | |
Show Lines
|
// ... lines 17 - 18 |
And... that's it! We'll do more work here later, but this is enough.
Creating the Form Object
Next, find your controller so we can render the form. Start by saying $form =
and using a shortcut: $this->createForm()
. Pass the class that you want to create: ArticleFormType::class
. I'll delete the return
response stuff and, instead, render a template with return $this->render('article_admin/new.html.twig')
. To render the form, we need to pass that in. Let's call the variable articleForm
and set it to - this is tricky - $form->createView()
. Yep: don't pass the $form
object directly to Twig: always call createView()
. This transforms the Form object into another object that is super good at rendering forms and telling funny stories at parties.
Show Lines
|
// ... lines 1 - 12 |
class ArticleAdminController extends AbstractController | |
{ | |
Show Lines
|
// ... lines 15 - 18 |
public function new(EntityManagerInterface $em) | |
{ | |
$form = $this->createForm(ArticleFormType::class); | |
return $this->render('article_admin/new.html.twig', [ | |
'articleForm' => $form->createView() | |
]); | |
} | |
Show Lines
|
// ... lines 27 - 35 |
} |
Rendering the Form
To create the template, I'll cheat! Ha! Thanks to the Symfony plugin, I can put my cursor on the template name, hit alt+enter
, click "Create Twig Template" and hit enter again to confirm the location. There's no real magic here: that just created the file for us at templates/article_admin/new.html.twig
.
Oh, and you might remember from previous tutorials that, in addition to the normal base.html.twig
, we also have a content_base.html.twig
, which gives us a little bit of real markup and a content_body
block that we can override. Let's use that: {% extends 'content_base.html.twig %}
and then, override the block content_body
, with {% endblock %}
. Add an <h1>Launch a new Article</h1>
with, of course, a rocket emoji! Zoom!
{% extends 'content_base.html.twig' %} | |
{% block content_body %} | |
<h1>Launch a new Article! ?</h1> | |
Show Lines
|
// ... lines 5 - 10 |
{% endblock %} |
To render the form, we get to use a few special form rendering functions: {{ form_start() }}
and pass that the articleForm
variable. At the end {{ form_end(articleForm }}
. And in the middle, {{ form_widget(articleForm) }}
. Oh, and for the submit button, you can build this into your form class, but I prefer to add it manually: <button type="submit">
, some classes: btn btn-primary
, and then Create
!
{% extends 'content_base.html.twig' %} | |
{% block content_body %} | |
<h1>Launch a new Article! ?</h1> | |
{{ form_start(articleForm) }} | |
{{ form_widget(articleForm) }} | |
<button type="submit" class="btn btn-primary">Create!</button> | |
{{ form_end(articleForm) }} | |
{% endblock %} |
And... we're done! We create a form class, create a Form object from that in the controller, pass the form to Twig, then render it. We'll learn a lot more about these rendering functions. But, more or less, form_start()
renders the opening form tag, form_end()
renders the form closing tag... plus a little extra magic, and form_widget()
renders all of the fields.
Try it! Find your browser and refresh! Woohoo! Just like that, we have a functional form. Sure, it's a bit ugly - but that will be super easy to fix. Before we get there, however, we need to talk about handling the form submit.
77 Comments
Hey davidmintz
Not sure about eta on new forms or even will be it released or no, however from my point of view nothing changed, everything mentioned here can be used in newer Symfony version, so the main ideas are still relevant
Cheers
how about adding comments ? with AJAX :D
Hey Ad F.!
That's an interesting idea! In theory, it's not really different than a normal form submit, but perhaps it's different enough that it's worth covering. Is there any part of this specifically that you're most interested in?
Cheers!
well, you have covered pretty much everything except comments, or how to add comments to an article as logged in user via ajax, or APi
when adding a comment, the author of post should be notified ... more complex stuff ...
or working with multiple forms.
subscribe to a topic & when someone post a new article on space bar, those subscribers will get a email notification...
there are plenty of stuff
will be a great idea to add those features to the course
Yeah that would be great!
If using Symfony > 4 (7 is the current), make sure to use $form
and not $form->createView()
when rendering:
'articleForm' => $form
instead of
'articleForm' => $form->createView()
Otherwise form errors are not displayed.
Hope this saves fellow devs who also do not read the manual, the time I wasted.
Reference: https://symfony.com/doc/current/forms.html#rendering-forms
Hey Radu,
Welcome to the club! ;) - Yea, that's a change in a recent Symfony version, you don't have to call createView()
anymore
Cheers!
The code code requires php 7.1, and, as in all 7.1 courses, I cannot run composer because the latest version does not support PHP <7.2.5, and I do not want to downgrade/upgrade composer all of the time.
Do you know of a workaround? I have PHP 7.1, my problem is with composer.
TIA,
François
Hey @Francois!
Yea, this tutorial - even if the contents are still valid - is showing its age BIG time. But, have you tried the code with PHP 7.2 or 7.3? Iirc (and this shows when you hover over the code download), this tutorial requires ^7.1.3
- which means 7.1.3 or higher but less than 8. So 7.2, 7.3 should work. But let me know if you run into any issus.
Cheers!
Hi
Very useful code. However, Will the course code works with Symfony 6 and PHP 8 ? Anyone tested that?
Regards
Hey t5810,
This course is based on Symfony 4. but the concepts we're teaching in this course are still valid for today. So, you can follow this course on a fresh Symfony 6 and PHP 8 for sure. You may notice some little differences but they all should be covered in the video and in the scripts with notes. However, in case you are stuck somewhere following this course on Symfony 6 - let us know in the comments below the video and our support team will help you ;)
Cheers!
Hi Victor
Thanks for your prompt and detailed answer. Today I make several attempts to amend the composer.json in attempt to install Symfony 6.2, but all my attempts failed.
Which are the steps that you would follow when you would like to watch old course but using Symfony 6? I would really like to learn this process, since all those dependencies when they start to hit your console with errors, one feel fairly helpless. If that is too much for explaining, can you point me to some resource where I can learn this technique?
On the end, it would be great if you symfony casts can have 2 json file in version tabs in older courses, one the original, and other with the latest version.
Regards
Hey t5810,
Oh, that's a bit tricky to upgrade old courses. The ideal would be to upgrade the old course by yourself... but upgrading major version, i.e. from 5 -> 6 may require a lot of work. Most probably it won't work after just installing the proper Symfony-6 dependencies because you also have to upgrade recipes, change BC breaks, etc.
So, the easiest would be to download a fresh new empty Symfony version from the official website, e.i following the steps from here: https://symfony.com/download - and then try to apply the changes you see from the video. You may need some prerequisites - like controller/template - but you can steal some code from the course. It's still not perfect as requires from you more changes, but that's how it is possible to follow it.
Or, as the 3rd option, you could try to follow the course on your own personal project, but it also may require some additional steps, like fetching some code from the course project.
I hope this helps!
Cheers!
Hi Victor
Thanks for the useful ideas, I will see what it suites me best. Maybe I will first watch the course as is, and then I will try to implement the knowledge on some my mini demo project.
Thanks.
Hey t5810,
Yeah, this is also a good idea! When you finished the whole course - you will have a lot of ideas about what exactly and where you could apply in your own project, so it's not the perfect user experience we would like our customers to have while watching our courses... but definitely a valid option for older tutorials.
Cheers!
Hey,
when i try to load then new article i get this error message,
Could not load type "App\Controller\ArticleFormType": class does not exist.
* @Route("/admin/article/new", name="admin_article_new")
* @IsGranted("ROLE_ADMIN_ARTICLE")
*/
public function new (EntityManagerInterface $em)
{ $form = $this->createForm(ArticleFormType::class);
return $this->render('article_admin/new.html.twig', [
'articleForm' => $form->createView() ]);
}
// ...
}
But it's there. What could be the reason? Thanks in advance.
Jimmy
Hey Jimmy!
Look closer to the namespace in the error message :) It seems like you missed to use the correct namespace somewhere, I suppose you form type is located inside src/Form/
? So, its namespace should be App\Form\ArticleFormType
- check that file first, and make sure its namespace matches the file structure. Then, check the controller where you use that form - I bet it misses the use statement :)
Cheers!
Hey,
thank you. I've already found it on my own. But i will have one question or more in the future 😊.
Cheers, Jimmy
Hey Jimmy,
Awesome, well done! Feel free to update or delete your comments that were not answered by the support yet in the future if it's not relevant anymore - it will unload our support a bit :)
Cheers!
Hello, I would like to go throw this course but in initial stage I have some issue with symfony 4 and php version which aren't compatible. Is the any solution for it?
Hey Alexander!
What problem exactly do you have? We would be happy to help you with it. Please, notice that this course isn't compatible with PHP 8, so if you want to follow this - there're a few options:
- Downgrade your PHP version to at least 7.4 (or you can install it additionally, like having both PHP 7.4 and 8.x versions. Or also there's an opportunity to use Docker as well)
- Or download the course code and try to upgrade it to the PHP version you're using locally. But it might require some extra work from you unfortunately. Though we have a course about upgrading that might help you with this: https://symfonycasts.com/sc...
- Or, you can download a fresh new Symfony version instead and try to apply all the changes we're showing in this course on it. It might be easier than the previous option, because you won't need to deal with upgrading at all. And of course, if you get stuck following this course on a newer Symfony version - let us know in the comments below the video and we will definitely help you to go further!
Cheers!
Hey Victor
I'll try to downgrade PHP version on additional laptop to 7.1
Thanks for fast reply)
Hey Alexander,
Great! Though I would suggest to upgrade to 7.4 instead - it should fit well, and not that ancient as 7.1 ;)
Cheers!
Downloaded project folder. Followed README, installed all accordingly: PHP72 version:
TypeError
array_key_exists(): Argument #2 ($array) must be of type array, Symfony\Component\Debug\Exception\FlattenException given
Hi, there , I have a question, I have multiple connections to different entities manager, and I want to create a form without using the default EM, by the way in the form I want to create a Combo box with Entity that is in an other EM, but I have this error when I'm creating the view of the form "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'em_default.table_in_other_em' doesn't exist"
Hey Francisco,
Could you show the code how you add that Combo box to your form type? Well, EntityType has a specific field called "em": https://symfony.com/doc/cur... - you can specify what entity manager to use. Or, you can easily inject any entity manager to the form type and use it inside.
I hope this helps!
Cheers!
Hi, victor I have injected the entity manager to the form, but I still have problems with the create view.
This is my code:
doctrine.yml
doctrine:
dbal:
default_connection: default
connections:
default:
# configure these for your database server
url: '%env(resolve:DATABASE_URL1)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
conn2:
# configure these for your database server
url: '%env(resolve:DATABASE_URL2)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
conn3:
# configure these for your database server
url: '%env(resolve:DATABASE_URL3)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
conn4:
# configure these for your database server
url: '%env(resolve:DATABASE_URL4)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
conn5:
# configure these for your database server
url: '%env(resolve:DATABASE_URL5)%'
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
Controller:
public function new()
{
$obj = new Entity_of_conn5();
$form = $this->createForm(Entity_of_conn5::class, $obj);
return $this->render('template.html.twig',
['form' => $form->createView()]
);
}
Form:
public function __construct(ManagerRegistry $managerRegistry)
{
$this->em = $managerRegistry->getManager('conn5');
$this->class = $this->em->getRepository(Entity1_of_conn5::class);
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('component1',EntityType::class,
array(
'class' => $this->class->getClassName(),
'label' => 'component1',
'required' => true,
'mapped' => false,
'placeholder' => 'Select',
'attr' => array( 'class' => 'form-control',
'placeholder' => 'component1',
),
)
)
->add('component2',ChoiceType::class,
array(
'choices' => ['OP1' => 'op1', 'OP2' => 'op2', 'OP3' => 'op3', 'OP4' => 'op4'],
'label' => 'component2',
'placeholder' => 'Select',
'required' => true,
'mapped' => false,
'attr' => array( 'class' => 'form-control selectpicker',
'placeholder' => 'component2',
),
)
)
->add('component3',ChoiceType::class,
array(
'choices' => ['OP1' => 'op1', 'OP2' => 'op2'],
'label' => 'component3',
'placeholder' => 'Select',
'required' => true,
'mapped' => false,
'attr' => array( 'class' => 'form-control selectpicker',
'placeholder' => 'component3',
),
)
)
->add('component4',ChoiceType::class,
array(
'choices' => ['OP1' => 'op1', 'OP2' => 'op2', 'OP3' => 'op3'],
'label' => 'component4',
'placeholder' => 'Select',
'required' => true,
'mapped' => false,
'attr' => array( 'class' => 'form-control selectpicker',
'placeholder' => 'component4',
),
)
)
->add('component5', TextType::class,
array(
'label' => 'component5',
'mapped' => false,
'attr' => array(
'class' => 'form-control',
'placeholder' => 'component5',
)
)
)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Entity_of_conn5::class,
'entity_manager' => $this->em
]);
}
<blockquote>ERROR: "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'em_default.Entity1_of_conn5' doesn't exist"</blockquote>
Hey Francisco,
Thanks for sharing more data with me! First of all, the line "$this->class = $this->em->getRepository(Entity1_of_conn5::class);" I'd recommend to rename to "$this->conn5Repository = $this->em->getRepository(Entity1_of_conn5::class);" or something like this as getRepository() method clearly returns the repository to you.
And once again, as I mentioned in my previous comment, you have to set that "em" option for your EntityType, I still don't see you did it. I.e. change to something like this:
builder
->add('component1',EntityType::class, [
'class' => $this->class->getClassName(),
'em' => $this->em,
// or I believe you can set just EM name here like: 'em' => 'conn5',
// Other options
])
// Other fields...
I hope it helps :)
Cheers!
Thank you for your help victor it works!
Hey Francisco,
Awesome! Thank you for confirming it works :)
Cheers!
I'm building an application with MySql that have big entity, with about ~ 200 fields, and I'm looking for some best practices to handle this.
And I expect to have at least 5000 records in entity per year.
Which is the best way to improve performance in my case? Keep all fields in the same entity, or split in two or more with a relation OneToOne?
I know that it depends in how I will work with this data, so i'm asking in a general way situation.
Hey Dmitriy,
It's really hard to answer, I'd recommend you to grab Blackfire.io profile tool and check your current performance. If it works well for you and you're happy with times it shows - great, nothing should be complicated probably. But you're right, it depends on how you use those data. If you almost always need all those fields - it probably better to keep them in the same entity instead of making OneToOne relations, as you will avoid joins in this case.
But if you're just looking for a way of "group" fields together - you can think about moving them to traits, like different traits would contains some fields that are logically grouped. This way you will group them, so probably it would be easier for you to work with all those fields, but you still will have them on the same entity.
But about performance - it is really difficult to say at the first glance, and very easy to check with profiling tools as Blackfire.io that I would recommend you.
I hope this helps!
Cheers!
What was the tip for the rocket image ?
Hey Joseph T.
If you work with PhpStorm, there are plugins that you can use to add emojis into your templates. Here you can see the full list of emojis https://www.w3schools.com/c...
Cheers!
Couse code does not work "flawlessly" with PHP 7.4+ a lot of dependencies have: PHP Deprecated: Array and string offset access syntax with curly braces is deprecated
Hey CISVHenriksen
Honestly we don't recommend to run composer update
on course code, because it can bring unexpected result or code behaviour. But sometimes we do update internal dependencies when something became incompatible. It's a very complex process and that is why deprecations are not the reason for complex updates.
If you have something really not working in course code, please report us back with details.
Cheers!
Hi, I have an interesting problem with this. For another project it was not an issue but downloading this course files there is an issue with the form. When the form is rendered, there is a mistake that <div id="article_form"> renders as <divid="article_form">. Therefore, broking the whole HTML code. The code is just as downloaded from your course. Do you have an idea why is it so?
Hello Mārtiņš C.
Thanks for reporting this issue, it was just fixed, so you only need to re-download course code and continue crushing this course! If you are interested it was caused by twig library version.
Cheers!
Thanks!
Hey Mārtiņš C.
Just to clarify do you use PHP 7.4?
Cheers!
I have a problem launching project. Opening web I get red Symfony error window: "PDOException >PDOException> DriverException", "An exception occurred in driver: could not find driver". In server terminal i see: many lines with PHP Deprecated: Array and string offset access syntax with curly braces is deprecated in /home/.../code-symfony-forms/start/vendor/michelf/php-markdown/Michelf/Markdown.php on line [955...1801] and MarkdownExtra.php line [215...1469] and red error: 127.0.0.1:59528 [500]: GET /
That's exactly right - thanks for posting Dameer! This error means that your PHP install doesn't have the PDO extension. How you install this depends on your system - you can use apt-get in Ubuntu or brew on a Mac (if you're using Brew).
Let us know if that helps!
Cheers!
Hey Mantas Z.
Can you provide more details? Like php version? MySQL version? Also can you check if mysql extension is enabled in PHP.
Cheers!
I have a problem when I download the new project in the beginning of the video. I have this error:
Argument 1 passed to Symfony\Component\Security\Core\Authentication\Token\AbstractToken::__unserialize() must be of the type array, string given, called in /Applications/MAMP/htdocs/EmarhAgence/vendor/symfony/security-guard/Token/PostAuthenticationGuardToken.php on line 87
This what happens when I try to connect me via the login page.
"Houston: no signs of life"
Start the conversation!
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
}
}
Not to be a whiner but I notice this is pretty dated. WIll there be an update, or are forms really too 2016? :-)