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!
04.
Bind Your Form to a Class
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!
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
}
}
16 Comments
Hallo, i can't see the form template.
twig.yaml:
<!DOCTYPE html>
<html lang="it">
</html>
{% extends 'base.html.twig' %}
{% block body %}
{% endblock %}
<!DOCTYPE html>
<html lang="it">
</html>
`I'm using symfony 5 with my own project (not spacebar).
Any suggestions?
Thanks,
Giacomo.
Hey Giacomo,
> i can't see the form template.
What do you mean exactly? You don't see styles for the bootstrap4 styled form? If so - then yes, you have to include "bootstrap.min.css" css file in order to apply those styles. Symfony form theme gives you only CSS classes, but actual styles should be included on the page by you.
Cheers!
Thank you!
I included boostrap CDN and that worked!
just out of curiosity, how can use boostrap after install with composer, instead of link che CDN?
Hey Giacomo,
Fairly speaking I don't know if Bootstrap can be installed with Composer :p Even if it could - you can't just link it from vendor/ dir, you have to copy it to the public/ dir somehow. Usually, there's a special bundles for it like symfony/assetic-bundle: https://github.com/symfony/... - but it was deprecated in favor of brand new WebpackEncoreBundle. You can learn more about Webpack Encore in our course here: https://symfonycasts.com/sc... - this is an amazing tool for your assets with bunch of features.
Cheers!
Hi SymfonyCasts! I have a completely custom form rendered in my twig:
I have no idea on how to submit the form response!
Any help?
Hey Shane,
Ha, good question! :) The button should have type="submit" to be able to submit the form, try to add this special type to your button and browser will know that it should send the form on that button hit, i.e.:
Cheers!
Hi,
is there a way to add an "a"-tag, or any other, to the label in the FormType??
Hey Oliver W.
By default Twig escapes your strings to avoid any HTML injection, if you really want to print some HTML you need to use the
rawfilter.Just do this:
[ 'label' => '<a href="#">....']|rawDocs reference: https://twig.symfony.com/doc/2.x/filters/raw.html
Cheers!
Hi MolloKhan
thx for your assistance but I need to do this in my form declaration not in the template!?
For now I made it in the template with a manual <label> combined with {{ form_widget() }} and {{ form_errors() }}. That's more work but I know what to do.
That may work but there are other 2 options depending on your needs. If you want to write the HTML inside the FormType class, then you will have to override the label value inside the template so you can use the
rawfilter. Something like this:or, just write the HTML inside the form template and you wouldn't have to use the raw filter
Cheers!
Hi, after adding in ArticleFormType:
in ArticleAdminController->new in
when i try to submit form i'm getting error:
<br />The property "App\Entity\Article::$title" is not readable because it is typed "string". You should initialize it or declare a default value instead.<br />ok, I have managed to fixed it. I added previously types to fields in Article Entity and they just needed to be initialized.
Hey Jacek
That's great that you fixed it. How do you think is it true that sometimes you can fix anything after asking about it? :) For example I'm always find a way to fix issue, after asking colleague :)))
Cheers!
Yep, it's the rubber duck debugging method :) https://en.wikipedia.org/wi...
Thanks for great tutorial :)
Hello, I have a drop down list of EntityType, i want to display a list grouped by family (so using form group) :
` ->add('idEtatMatriceDetails', EntityType::class,[
....`
under FAMILY_ONE, i can have several options, however, some of these options are disabled (there is a column for that flag), so I want to filter these options, how can I do this in the closure? can I craft a special getter in the entity that filter those values? Thank you
Hey Yvon,
I see you mentioned that $choice in that closure is an instance of EtatMatriceDetails entity, so yes, I think you can create a new method in that entity with the logic and use it inside the closure to group them, or you can do the same logic directly in that closure. Have you tried it? Did you have any problems with this implementation?
Cheers!
"Houston: no signs of life"
Start the conversation!