gstreamer0.10-ffmpeg
gstreamer0.10-plugins-good
packages.
Our form has an input text
field and a textarea
... which is interesting because, in our form class, all we've done is give each field a name. We... actually have not said anything about what "type" of field each should be.
But, we now know that, because we've bound our form to our entity, the form type "guessing" system is able to read the Doctrine metadata, notice that content
looks like a big field, and "guess" that it should be a textarea.
Field type guessing is cool! But... it's not meant to be perfect: we need a way to take control. How? It turns out that the add()
method has three arguments: the field name, the field type and some options.
For title
pass, TextType::class
- the one from Form\Extensions
. Go back to the form refresh and... absolutely nothing changes! Symfony was already "guessing" that this was a TextType
field.
Google for "Symfony forms", click into the Symfony form documentation and find a section called "Built-in Field Types". Woh. It turns out that there are a ton of built-in field types. Yep, there's a field type for every HTML5 field that exists, as well as a few other, special ones.
Click into TextType
. In addition to choosing which type you need, every type is super configurable. Many of these options are global, meaning, the options can be used for any field type. A good example is the label
option: you can set a label
option for any field, regardless of its type.
But other options are specific to that field type. We'll see an example in a minute.
Check out this help
option: we can define a "help" message for any field. That sounds awesome! Back in the form class, add a third argument: an options array. Pass help
and set it to "Choose something catchy".
... lines 1 - 10 | |
class ArticleFormType extends AbstractType | |
{ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('title', TextType::class, [ | |
'help' => 'Choose something catchy!' | |
]) | |
... lines 19 - 20 | |
} | |
... lines 22 - 28 | |
} |
Let's go check it out! Refresh! I like that! Nice little help text below the field.
This finally gives us a reason to check out one of the killer features of Symfony's form system. Look down at the web debug toolbar and find the little clipboard icon. Click that.
Yes! Say hello to the form profiler screen! We can see our entire form and the individual fields. Click on the title
field to get all sorts of information about it. We're going to look at this several more times in this tutorial and learn about each piece of information.
Under "Passed Options" you can see the help
option that we just passed. But, what's really cool are these "Resolved Options". This is a list of every option that was used to control the rendering & behavior of this one field. A lot of these are low-level and don't directly do anything for this field - like the CSRF stuff. That's why the official docs can sometimes be easier to look at. But, this is an amazing way to see what's truly going on under the hood: what are all the options for this field and their values.
And, yea! We can override any of these options via the third argument to the add()
method. Without even reading the docs, we can see that we could pass a label
option, label_attr
to set HTML attributes on your label, or a required
option... which actually has nothing to do with validation, but controls whether or not the HTML5 required
attribute should be rendered. More on that later.
Anyways, let's add another, more complex field - and use its options to totally transform how it looks and works.
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"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
"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/console": "^4.0", // v4.1.6
"symfony/flex": "^1.0", // v1.17.6
"symfony/form": "^4.0", // v4.1.6
"symfony/framework-bundle": "^4.0", // v4.1.6
"symfony/orm-pack": "^1.0", // v1.0.6
"symfony/security-bundle": "^4.0", // v4.1.6
"symfony/serializer-pack": "^1.0", // v1.0.1
"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/profiler-pack": "^1.0", // v1.0.3
"symfony/var-dumper": "^3.3|^4.0" // v4.1.6
}
}