Skip to content

Commit 080e516

Browse files
committed
minor #17092 [Form] Better explaining what getParent actually does (ThomasLandauer)
This PR was merged into the 5.4 branch. Discussion ---------- [Form] Better explaining what `getParent` actually does Main reason for changing this is to stop presenting `getParent()` of being the "default" (or normal) way of doing it. * I removed the "PHP extension" note-box, cause the (new) text for `getParent()` makes it clear what this does. * I moved `finishView()` one paragraph up (didn't change anything) * Moved all `getParent`-related stuff into its paragraph, and moved it to the top (since it's the most important question in the beginning) Commits ------- 1c9b524 Better explaining what `getParent` actually does
2 parents 3e1074c + 1c9b524 commit 080e516

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

form/create_custom_field_type.rst

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,11 @@ By convention they are stored in the ``src/Form/Type/`` directory::
5555
}
5656
}
5757

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.)
6161
The resulting form type is a choice field with predefined choices.
6262

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-
7163
Now you can add this form type when :doc:`creating Symfony forms </forms>`::
7264

7365
// src/Form/Type/OrderType.php
@@ -123,44 +115,34 @@ convenient to extend instead from :class:`Symfony\\Component\\Form\\AbstractType
123115
// ...
124116
}
125117

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-
131118
These are the most important methods that a form type class can define:
132119

133120
.. _form-type-methods-explanation:
134121

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+
135129
``buildForm()``
136130
It adds and configures other types into this type. It's the same method used
137131
when :ref:`creating Symfony form classes <creating-forms-in-classes>`.
138132

139133
``buildView()``
140134
It sets any extra variables you'll need when rendering the field in a template.
141135

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-
148136
``finishView()``
149137
When creating a form type that consists of many fields, this method allows
150138
to modify the "view" of any of those fields. For any other use case, it's
151139
recommended to use ``buildView()`` instead.
152140

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.
164146

165147
Defining the Form Type
166148
~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)