@@ -55,19 +55,11 @@ By convention they are stored in the ``src/Form/Type/`` directory::
55
55
}
56
56
}
57
57
58
- The methods of the ``FormTypeInterface `` are explained in detail later in
59
- this article. Here, ``getParent ()`` method defines the base type
60
- (``ChoiceType``) and ``configureOptions()`` overrides some of its options.
58
+ ``getParent()`` tells Symfony to take ``ChoiceType `` as a starting point,
59
+ then ``configureOptions ()`` overrides some of its options. (All methods of the
60
+ ``FormTypeInterface`` are explained in detail later in this article.)
61
61
The resulting form type is a choice field with predefined choices.
62
62
63
- .. note::
64
-
65
- The PHP class extension mechanism and the Symfony form field extension
66
- mechanism are not the same. The parent type returned in ``getParent()`` is
67
- what Symfony uses to build and manage the field type. Making the PHP class
68
- extend from ``AbstractType`` is only a convenient way of implementing the
69
- required ``FormTypeInterface``.
70
-
71
63
Now you can add this form type when :doc:`creating Symfony forms </forms>`::
72
64
73
65
// src/Form/Type/OrderType.php
@@ -123,44 +115,34 @@ convenient to extend instead from :class:`Symfony\\Component\\Form\\AbstractType
123
115
// ...
124
116
}
125
117
126
- When a form type doesn't extend from another specific type, there's no need to
127
- implement the ``getParent()`` method (Symfony will make the type extend from the
128
- generic :class:`Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType`,
129
- which is the parent of all the other types).
130
-
131
118
These are the most important methods that a form type class can define:
132
119
133
120
.. _form-type-methods-explanation:
134
121
122
+ ``getParent()``
123
+ When returning a (fully-qualified) class name here, Symfony will call each
124
+ method of that type (i.e. ``buildForm()``, ``buildView()``, etc.) and all its
125
+ type extensions, before calling the corresponding method of your custom type.
126
+ This is probably a good idea if you're just changing some details of an
127
+ existing type. To start from scratch, just omit ``getParent()``.
128
+
135
129
``buildForm()``
136
130
It adds and configures other types into this type. It's the same method used
137
131
when :ref:`creating Symfony form classes <creating-forms-in-classes>`.
138
132
139
133
``buildView()``
140
134
It sets any extra variables you'll need when rendering the field in a template.
141
135
142
- ``configureOptions()``
143
- It defines the options configurable when using the form type, which are also
144
- the options that can be used in ``buildForm()`` and ``buildView()``
145
- methods. Options are inherited from parent types and parent type
146
- extensions, but you can create any custom option you need.
147
-
148
136
``finishView()``
149
137
When creating a form type that consists of many fields, this method allows
150
138
to modify the "view" of any of those fields. For any other use case, it's
151
139
recommended to use ``buildView()`` instead.
152
140
153
- ``getParent()``
154
- If your custom type is based on another type (i.e. they share some
155
- functionality) add this method to return the fully-qualified class name
156
- of that original type. Do not use PHP inheritance for this.
157
- Symfony will call all the form type methods (``buildForm()``,
158
- ``buildView()``, etc.) of the parent type and it will call all its type
159
- extensions before calling the ones defined in your custom type.
160
-
161
- By default, the ``AbstractType`` class returns the generic
162
- :class:`Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType`
163
- type, which is the root parent for all form types in the Form component.
141
+ ``configureOptions()``
142
+ It defines the options configurable when using the form type, which are also
143
+ the options that can be used in ``buildForm()`` and ``buildView()``
144
+ methods. Options are inherited from parent types and parent type
145
+ extensions, but you can create any custom option you need.
164
146
165
147
Defining the Form Type
166
148
~~~~~~~~~~~~~~~~~~~~~~
0 commit comments