Skip to content

[Cookbook] Fix doc on Generic Form Type Extensions #5227

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
Closed
Show file tree
Hide file tree
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
31 changes: 23 additions & 8 deletions cookbook/form/create_form_type_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ extensions come in.

Form type extensions have 2 main use-cases:

#. You want to add a **generic feature to several types** (such as
adding a "help" text to every field type);
#. You want to add a **specific feature to a single type** (such
as adding a "download" feature to the "file" field type).
as adding a "download" feature to the "file" field type);
#. You want to add a **generic feature to several types** (such as
adding a "help" text to every "input text"-like type).

Copy link
Member

Choose a reason for hiding this comment

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

I like these 2 bullet points - they quickly answer the question "what can I do with this form type extension thing?". I think you removed them because it says "every field type", but that's effectively true (the button being the exception). I'd like to have these back, and let the last paragraph explain the edge case.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

👍

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 agree this was a great way to explain Foem Type Extensions.

I restored them but changed the example. I now use ""adding a "help" text to every "input text"-like type"". This seem to be closer to an actual use case but also more truthful regarding what can be done.

In both those cases, it might be possible to achieve your goal with custom
form rendering, or custom form field types. But using form type extensions
can be cleaner (by limiting the amount of business logic in templates)
and more flexible (you can add several type extensions to a single form
type).
It might be possible to achieve your goal with custom form rendering, or custom
form field types. But using form type extensions can be cleaner (by limiting the
amount of business logic in templates) and more flexible (you can add several
type extensions to a single form type).

Form type extensions can achieve most of what custom field types can do,
but instead of being field types of their own, **they plug into existing types**.
Expand Down Expand Up @@ -319,3 +318,19 @@ next to the file field. For example::

When displaying the form, if the underlying model has already been associated
with an image, you will see it displayed next to the file input.

Generic Form Type Extensions
----------------------------
Copy link
Member

Choose a reason for hiding this comment

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

How about:

Applying an Extension to All (Most) Fields

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 liked the "Generic" wording because it's very easy to search and provides a nice keyword. Plus it matches the text in the bullet points at the top of the article.

Copy link
Member

Choose a reason for hiding this comment

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

That's fine with me :). In the paragraph below, we should mention the 'getExtendedType' function specifically so they know where they'd exactly put 'text' or 'form' (maybe even show a small code block)


You can modify several form types at once by specifying their common parent
(:doc:`/reference/forms/types`). For example, several form types natively
available in Symfony inherit from the ``text`` form type (such as ``email``,
``search``, ``url``, etc.). A form type extension applying to ``text``
(i.e. whose ``getExtendedType`` method returns ``text``) would apply to all of
these form types.

In the same way, since **most** form types natively available in Symfony inherit
from the ``form`` form type, a form type extension applying to ``form`` would
apply to all of these. A notable exception are the ``button`` form types. Plus,
keep in mind that a custom form type which inherit neither ``form`` nor
``button`` could always be created.
68 changes: 2 additions & 66 deletions reference/dic_tags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -296,72 +296,8 @@ form.type_extension

**Purpose**: Create a custom "form extension"

Form type extensions are a way for you took "hook into" the creation of
any field in your form. For example, the addition of the CSRF token is done
via a form type extension
(:class:`Symfony\\Component\\Form\\Extension\\Csrf\\Type\\FormTypeCsrfExtension`).

A form type extension can modify any part of any field in your form. To
create a form type extension, first create a class that implements the
:class:`Symfony\\Component\\Form\\FormTypeExtensionInterface` interface.
For simplicity, you'll often extend an
:class:`Symfony\\Component\\Form\\AbstractTypeExtension` class instead of
the interface directly::

// src/Acme/MainBundle/Form/Type/MyFormTypeExtension.php
namespace Acme\MainBundle\Form\Type;

use Symfony\Component\Form\AbstractTypeExtension;

class MyFormTypeExtension extends AbstractTypeExtension
{
// ... fill in whatever methods you want to override
// like buildForm(), buildView(), finishView(), setDefaultOptions()
}

In order for Symfony to know about your form extension and use it, give
it the ``form.type_extension`` tag:

.. configuration-block::

.. code-block:: yaml

services:
main.form.type.my_form_type_extension:
class: Acme\MainBundle\Form\Type\MyFormTypeExtension
tags:
- { name: form.type_extension, alias: field }

.. code-block:: xml

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service
id="main.form.type.my_form_type_extension"
class="Acme\MainBundle\Form\Type\MyFormTypeExtension">

<tag name="form.type_extension" alias="field" />
</service>
</services>
</container>

.. code-block:: php

$container
->register(
'main.form.type.my_form_type_extension',
'Acme\MainBundle\Form\Type\MyFormTypeExtension'
)
->addTag('form.type_extension', array('alias' => 'field'))
;

The ``alias`` key of the tag is the type of field that this extension should
be applied to. For example, to apply the extension to any form/field, use
the "form" value.
For details on creating Form type extensions, read the cookbook article:
:doc:`/cookbook/form/create_form_type_extension`

.. _reference-dic-type_guesser:

Expand Down