Forms
26 Comments


When I tried to reload my new form I got exeption:
Expected argument of type "string", "AppBundle\Form\MovieType" given
I have changed :
$form = $this->createForm(new MovieType(), $movie);
for:
$form = $this->createForm(MovieType::class, $movie);
Got new:
Could not load type "text"
500 Internal Server Error - InvalidArgumentException
Your comments suggestions would be greatly appreciated
Hey Andrew!
You've already got the right fix actually! This tutorial was on Symfony 2, where we did things like "new MovieType" and ->add('someField', 'text'). In Symfony 3, change it to MovieType::class (you already did this) *and* make a similar change in your form class - e.g. ->add('someField', TextType::class); That'll fix things :).
Cheers!
I literally wrote this and then saw you solved the issue yourself :). Thanks for posting the detailed update! https://knpuniversity.com/s...


Does this still work. I am using Ubuntu 20.04 with latest version of PhpStorm with Symfony5 and I initiated the project with the full option
symfony new --full my_project.
I don't see any of the options in the new menu. Just one option for symfony service and when click on that nothing happens as well. I uninstalled and re-installed the symfony support plugin and enabled it as well for the project.

Hey Graymath technology
I believe Symfony plugin people dropped that feature because I can't do it either but the good news is you don't need it anymore, you can generate any boiler plate file by using the MakerBundle. It's a super handy bundle that you should give it a try https://symfony.com/doc/cur...
Cheers!


I did all as you do, but when my form is rendered the field 'Released at' does not display
'mm/dd/yyyy' inside , just empty. Could you give me, please, any hint how to fix it.
Hey Andrew!
You're probably doing it correctly :). Symfony renders this fields as an input type="date" field. *Some* browsers (like mine - Chrome) are smart enough to turn that into a "smart" field - that's what you're seeing. I (and Symfony) did *not* add that mm/dd/yyyy - my browser is adding that because it knows this is a date field. If you try Chrome, you will likely see that same behavior. But even if you don't, you can just fill in the field with something like 06/20/2000 and it will submit. If you'd like to have a fancy date picker in *all* browsers, you should use a JavaScript library to help the user fill in the right date (e.g. 06/20/2000).
Cheers!


Probably my Google Chrome is silly and don't displayt mm/dd/yyyy either.
From your point of view , is Chrome more advanced for web development now then Fox?
Best regards!
Ah, so strange! I mostly prefer Chrome because I like its native web inspector better! I *do* (for whatever reason) see it used by most developers, probably with Safari second.


Hello, first thanks to Andrew for the solution with the 500 error, I had the same problem.
I have another question, I have bootstrap under web/vendors/bootstrap, but the code has no effect. Inspect shows that the bootstrap classes are there, but the page looks the same as before, is it a path problem?
thanks.


Me again and sorry! Of course I needed to add the reference to the css in base.html.twig....


Hello there!!! i`m in love with Symfony and KNPU... I have a question... There is a way to render the fields of FORM in columns? like [ 'class'=>'col-md-3' ]??? I`m not founding where to change the variables... thanks!!!
Hi Raphael!
Ah, thank you so much!!!! I'm glad you love both things :p.
I might have an easy answer to your question: have you tried the bootstrap_3_horizontal_layout.html.twig form theme? It organizes labels and fields into columns: https://github.com/symfony/...
You can configure to use this form theme in the same way as the other bootstrap form theme.
Cheers!
Help! I'm not getting completion on the add type fields. eg in $builder->add('title','text') "text" is not completing. What is missing? So much going on so quickly I'm not sure what I might have missed. I tab completed the add call so assume all the right "usages" are in. But all I have are:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
OK. the code below doesn't match the video. The correct syntax for 3.2 is : $builder->add('title',TextType::class) which is in the code forms but not in the video. Or am I going doolally? ;)
Yo Richard ,
Yes, you're right, code blocks related to the Symfony forms mismatch the video. We updated code blocks to use the new Symfony 3 syntax, but not the video. Btw, we have a note about it at the beginning of this article: https://knpuniversity.com/s... ;)
Cheers!
Hey Richard ,
If you're on Symfony 3 - you should use `TextType::class` as you figured out below by yourself. The 'text' way is deprecated and doesn't work since Symfony 3.
Cheers!


The same exception was thrown when I tried to reload the form page (expected argument of type string ...).
I finally found a solution to fix it.
I REPLACED >> "$form = $this->createForm(new MovieType(), $movie);"
BY >> "form = $this->get('form.factory')->create(MovieType::class, $movie);"
I also used fully-qualified class name in the buildForm function (as described by there ) .
Yep, you guys are absolutely right - the "new MovieType" was one of the changes that was deprecated in 2.x and removed for Symfony 3.x (replaced with the MoveType::class form). That's the key change to make :).


I'm using PHPStorm on Ubuntu OS.
When I'm trying to create new Form or Controller nothing happens when I click on 'New->Form' or 'New->Controller'
Hey Paulius!
So, you have the New->Form and New->Controller options, but they don't do anything when you click them? Or are the options missing? If clicking them does nothing, that sounds like some sort of a bug with the Symfony plugin.
Hmm, I've never seen that before, unfortunately :(. You could try re-installing the plugin/phpstorm - it's the best advice I can give!


I have PHPstorm on my windows7 and symfony 3.
When I try (Ctrl + click) over 'widget' I do not get to Core/Type/DateType.php as you do but see "Can't find declaration to go to".
All the same for 'integer'. What can it be?
Hi Andrew!
Do you have the Symfony plugin installed and (more importantly) enabled for this project? That's the most likely cause - let me know! *Sometimes* things won't work, even when you have the plugin installed+enabled, but I don't have problems with forms usually.
Cheers!
I found the issue.
WAS OK BEFORE(sf 2.8):
$builder->add('title', 'text')
->add('samsCharacterName', 'text')
->add('isMainCharacter', 'checkbox', array(
'required' => false
))
->add('rating', 'integer')
->add('releasedAt', 'date', array(
'widget' => 'single_text',
));
SHOULD BE (from sf 2.8):
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
$builder->add('title', TextType::class)
->add('samsCharacterName', TextType::class)
->add('isMainCharacter', CheckboxType::class, array(
'required' => false
))
->add('rating', IntegerType::class)
->add('releasedAt', DateType::class , array(
'widget' => 'single_text',
));