Skip to content

[Form] Added explicit getParent() call in types inheritance mechanism #14588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions form/create_custom_field_type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,10 @@ By convention they are stored in the ``src/Form/Type/`` directory::
}
}

The ``configureOptions()`` method, which is explained later in this article,
defines the options that can be configured for the form type and sets the
default value of those options.

The ``getParent()`` method defines which is the form type used as the base of
this type. In this case, the type extends from ``ChoiceType`` to reuse all of
the logic and rendering of that field type.
The methods of the ``FormTypeInterface`` are explained in detail later in
this article. Here, ``getParent()`` method defines the base type
(``ChoiceType``) and ``configureOptions()`` overrides some of its options.
The resulting form type is a choice field with predefined choices.

.. note::

Expand Down Expand Up @@ -144,13 +141,27 @@ These are the most important methods that a form type class can define:

``configureOptions()``
It defines the options configurable when using the form type, which are also
the options that can be used in ``buildForm()`` and ``buildView()`` methods.
the options that can be used in ``buildForm()`` and ``buildView()``
methods. Options are inherited from parent types and parent type
extensions, but you can create any custom option you need.

``finishView()``
When creating a form type that consists of many fields, this method allows
to modify the "view" of any of those fields. For any other use case, it's
recommended to use instead the ``buildView()`` method.

``getParent()``
If your custom type is based on another type (i.e. they share some
functionality) add this method to return the fully-qualified class name
of that original type. Do not use PHP inheritance for this.
Symfony will call all the form type methods (``buildForm()``,
``buildView()``, etc.) of the parent type and it will call all its type
extensions before calling the ones defined in your custom type.

By default, the ``AbstractType`` class returns the generic
:class:`Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType`
type, which is the root parent for all form types in the Form component.

Defining the Form Type
~~~~~~~~~~~~~~~~~~~~~~

Expand Down