Skip to content

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

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

Closed
wants to merge 2 commits into from
Closed
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
37 changes: 25 additions & 12 deletions form/create_custom_field_type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,31 @@ check out the `ChoiceType`_ class.
extend from ``AbstractType`` is only a convenience way of implementing the
required ``FormTypeInterface``.

There are three methods that are particularly important:
There are a few methods that are particularly important:

.. _form-type-methods-explanation:

``getParent()``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description focuses on "what/how" instead of "when/why". It explains that this is "like PHP inheritance but implemented in PHP, because you can't use real PHP inheritance".

But I'm missing information about the "why" and "when" to use this:

  • Is it mandatory to define this method?
  • Which value should return this method? (the FQCN of a class implementing FormType??)
  • How should I use this? (e.g. if you create a form type similar to an existing one, extend from it ... otherwise, extend from the generic FormType ??)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a rewording.

This return the class name of the type configuring the form before. It
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

returns

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not really sure if I completely understand what the meaning of the "the form before" part is supposed to be.

means all the other methods below will be called with this parent type
and all its type extensions before calling the ones defined in your
custom type.
By default, all classes inherit the ``AbstractType::getParent()`` which
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"[...] all classes extend the AbstractType class [...]" or "[...] all classes inherit the AbstractType::getParent() method [...]"?

defines the ``FormType`` as parent type to re-use its options and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] as the parent type [...]

configuration.

``configureOptions()``
This defines options for your type that
can be used in ``buildForm()`` and ``buildView()``. Options are inherited
from parent types and parent types extension but you can create any custom
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] and parent type extensions, but [...]

option you need.

``buildForm()``
Each field type has a ``buildForm()`` method, which is where
you configure and build any field(s). Notice that this is the same method
you use to setup *your* forms, and it works the same here.
you configure and build any field(s) depending on options defined in
``configureOptions()``.
Notice that this is the same method you use to set up *your* forms, and it
works the same here.

``buildView()``
This method is used to set any extra variables you'll
Expand All @@ -83,18 +100,14 @@ There are three methods that are particularly important:
set) the ``multiple`` attribute on the ``select`` field. See
`Creating a Template for the Field`_ for more details.

``configureOptions()``
This defines options for your form type that
can be used in ``buildForm()`` and ``buildView()``. There are a lot of
options common to all fields (see :doc:`/reference/forms/types/form`),
but you can create any others that you need here.
``finishView``
Same as ``buildView`` but to configure the nested field views.
This method as no use with types that build compound forms.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method has no use [...]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method as no use with types that build compound forms.

By the way, isn't that the other way around?


.. tip::

If you're creating a field that consists of many fields, then be sure
to set your "parent" type as ``form`` or something that extends ``form``.
Also, if you need to modify the "view" of any of your child types from
your parent type, use the ``finishView()`` method.
If you're creating a field that consists of nested fields, then be sure to
set the parent type as ``FormType::class`` or one of its child.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] or one of its children.


The goal of this field was to extend the choice type to enable selection of the
shipping type. This is achieved by fixing the ``choices`` to a list of available
Expand Down