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!
23.
Custom Field Type
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
}
}
5 Comments
Hey,
How "Let's try it! Move over, refresh and... it actually works! It's a text field filled with the firstName of the current author." if in case of deleting the whole EntityType stuff we've have this:
`
$builder->...
`
Only this works correctly and there is no firstName at all or something connected to the UserEntity.
What I've missed?
Hey MASQUERADE promotion!
Great question. Here is what happens behind the scenes at this exact moment:
1) Symfony starts rendering the "author" field
2) It sees that the Article.author field is populated by a User object
3) It then looks at the UserSelectTextType and sees that its parent is TextType::class. That means it should render as an
input type="text"field4) It renders the
<input type="text">field. For thevalueattribute, it literally just tries to print theauthorfield - so it literally tries to print the User object that is on that field. Thanks to the__toString()method on the User object, instead of an error, the firstName is printed.So, you're right - the form does not have ANY configuration yet to connect it to the User entity. BUT, the Article.author property IS a User object, and so the form tries to render using that value.
I hope this helps!
Cheers!
ok. thanks. Just didn't have that written, while had the __toString() and then wrote you.
I am facing an unknown issue here, I create the custom type same as in the tutorial, User class already have the __toString method, I changed the ArticleFormType's author field as per instructions, my form rendering correctly, but with no value (author firstname is not there), I checked that value in the custom transformer, null was passed there. If I remove the custom type, and leave it to symfony to guess the type, then it renders correctly as dropdown with all user-names, but even if I changed it to TextType, it doesn't show anything (just empty field).
What could be the reason?
Hey Wazir Khan!
Sorry for my super slow reply! Hmm... what does your
__toString()method look like? And what does yourArticleFormTypelook like? I'm looking at a few things... and they all point to this being some, small subtle typo somewhere in your code. It could be in__toString()... though I'm pretty sure that when you let Symfony "guess" the type, it would also use that (but it's actually showing the usernames? That's curious). Anyways, if you're still having the issue, post some code here and we'll get to the bottom of it.Cheers!
"Houston: no signs of life"
Start the conversation!