I've been working with Symfony Framework for some time now and I've always wondered this questions.
Why are form elements called Types in Symfony Framework?
Thank's a lot for explaining to me.
This is because of the fact that you can chain them as you like. If you create a custom form type containing all the fields you would like to display on a specific page (like i do normally for every form i am creating) then you can reuse this form in other forms.
One example could be that you have a form type for addresses. In the address view you only use the address form for editing addresses. But now think you have a page to create a new customer. The customer entity for instance has a relation to a address. Instead of repeating the necessary fields in this form type again you can simply specify one single field called address with the type of your already defined address type. Now the address form gets embedded directly at this position and also ensures to create a object of the correct type to set this on the user address entity property later on when you bind the request to the form.
Now back to the original question: If you understand this concept of chaining forms in each other we can have a look at the DateType. The DateType can be configured as single HTML5 input field, or it shows up as three separate select fields (one for year, month and date). The concept here is exactly the same. The DateType defines fields each of them as ChoiceType. Additionally to this the DateType defines converters that are trigger when binding data to the form to ensure that da PHP DateTime object is correctly converted to those three separate values and the other way around.
We can also imagine to go one level higher and imagine a DateTimeType. Crating such a type is pretty straightforward if you have a DateType and TimeType already at hand. You simply need to define two fields that are reusing those types and do some converting.
For the ChoceType we also have the example that it internally uses a RadioType and CheckboxType and SelectType to display different flavors of choice forms.
So as you see the difference between what we would call a complete form part and a single field is very marginal and theoretical. That's why symfony don't make any difference in naming here.
Some further links to the documentation: How to Create a Custom Form Field Type
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With