Skip to content

[Form] Better explaining what getParent actually does #17092

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 1 commit into from
Oct 4, 2022
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
48 changes: 15 additions & 33 deletions form/create_custom_field_type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,11 @@ By convention they are stored in the ``src/Form/Type/`` directory::
}
}

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.
``getParent()`` tells Symfony to take ``ChoiceType`` as a starting point,
then ``configureOptions()`` overrides some of its options. (All methods of the
``FormTypeInterface`` are explained in detail later in this article.)
The resulting form type is a choice field with predefined choices.

.. note::

The PHP class extension mechanism and the Symfony form field extension
mechanism are not the same. The parent type returned in ``getParent()`` is
what Symfony uses to build and manage the field type. Making the PHP class
extend from ``AbstractType`` is only a convenient way of implementing the
required ``FormTypeInterface``.

Now you can add this form type when :doc:`creating Symfony forms </forms>`::

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

When a form type doesn't extend from another specific type, there's no need to
implement the ``getParent()`` method (Symfony will make the type extend from the
generic :class:`Symfony\\Component\\Form\\Extension\\Core\\Type\\FormType`,
which is the parent of all the other types).

These are the most important methods that a form type class can define:

.. _form-type-methods-explanation:

``getParent()``
When returning a (fully-qualified) class name here, Symfony will call each
method of that type (i.e. ``buildForm()``, ``buildView()``, etc.) and all its
type extensions, before calling the corresponding method of your custom type.
This is probably a good idea if you're just changing some details of an
existing type. To start from scratch, just omit ``getParent()``.

``buildForm()``
It adds and configures other types into this type. It's the same method used
when :ref:`creating Symfony form classes <creating-forms-in-classes>`.

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

``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. 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 ``buildView()`` instead.

``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.
``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. Options are inherited from parent types and parent type
extensions, but you can create any custom option you need.

Defining the Form Type
~~~~~~~~~~~~~~~~~~~~~~
Expand Down