Skip to content

Commit a6bc497

Browse files
lemoinemxabbuh
authored andcommitted
[Cookbook] Fix doc on Generic Form Type Extensions
1 parent 68db46c commit a6bc497

File tree

2 files changed

+25
-74
lines changed

2 files changed

+25
-74
lines changed

cookbook/form/create_form_type_extension.rst

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ extensions come in.
1414

1515
Form type extensions have 2 main use-cases:
1616

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

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

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

320319
When displaying the form, if the underlying model has already been associated
321320
with an image, you will see it displayed next to the file input.
321+
322+
Generic Form Type Extensions
323+
----------------------------
324+
325+
You can modify several form types at once by specifying their common parent
326+
(:doc:`/reference/forms/types`). For example, several form types natively
327+
available in Symfony inherit from the ``text`` form type (such as ``email``,
328+
``search``, ``url``, etc.). A form type extension applying to ``text``
329+
(i.e. whose ``getExtendedType`` method returns ``text``) would apply to all of
330+
these form types.
331+
332+
In the same way, since **most** form types natively available in Symfony inherit
333+
from the ``form`` form type, a form type extension applying to ``form`` would
334+
apply to all of these. A notable exception are the ``button`` form types. Plus,
335+
keep in mind that a custom form type which inherit neither ``form`` nor
336+
``button`` could always be created.

reference/dic_tags.rst

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -296,72 +296,8 @@ form.type_extension
296296

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

299-
Form type extensions are a way for you took "hook into" the creation of
300-
any field in your form. For example, the addition of the CSRF token is done
301-
via a form type extension
302-
(:class:`Symfony\\Component\\Form\\Extension\\Csrf\\Type\\FormTypeCsrfExtension`).
303-
304-
A form type extension can modify any part of any field in your form. To
305-
create a form type extension, first create a class that implements the
306-
:class:`Symfony\\Component\\Form\\FormTypeExtensionInterface` interface.
307-
For simplicity, you'll often extend an
308-
:class:`Symfony\\Component\\Form\\AbstractTypeExtension` class instead of
309-
the interface directly::
310-
311-
// src/Acme/MainBundle/Form/Type/MyFormTypeExtension.php
312-
namespace Acme\MainBundle\Form\Type;
313-
314-
use Symfony\Component\Form\AbstractTypeExtension;
315-
316-
class MyFormTypeExtension extends AbstractTypeExtension
317-
{
318-
// ... fill in whatever methods you want to override
319-
// like buildForm(), buildView(), finishView(), setDefaultOptions()
320-
}
321-
322-
In order for Symfony to know about your form extension and use it, give
323-
it the ``form.type_extension`` tag:
324-
325-
.. configuration-block::
326-
327-
.. code-block:: yaml
328-
329-
services:
330-
main.form.type.my_form_type_extension:
331-
class: Acme\MainBundle\Form\Type\MyFormTypeExtension
332-
tags:
333-
- { name: form.type_extension, alias: field }
334-
335-
.. code-block:: xml
336-
337-
<?xml version="1.0" encoding="UTF-8" ?>
338-
<container xmlns="http://symfony.com/schema/dic/services"
339-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
340-
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
341-
342-
<services>
343-
<service
344-
id="main.form.type.my_form_type_extension"
345-
class="Acme\MainBundle\Form\Type\MyFormTypeExtension">
346-
347-
<tag name="form.type_extension" alias="field" />
348-
</service>
349-
</services>
350-
</container>
351-
352-
.. code-block:: php
353-
354-
$container
355-
->register(
356-
'main.form.type.my_form_type_extension',
357-
'Acme\MainBundle\Form\Type\MyFormTypeExtension'
358-
)
359-
->addTag('form.type_extension', array('alias' => 'field'))
360-
;
361-
362-
The ``alias`` key of the tag is the type of field that this extension should
363-
be applied to. For example, to apply the extension to any form/field, use
364-
the "form" value.
299+
For details on creating Form type extensions, read the cookbook article:
300+
:doc:`/cookbook/form/create_form_type_extension`
365301

366302
.. _reference-dic-type_guesser:
367303

0 commit comments

Comments
 (0)