Skip to content

Commit fe6d763

Browse files
committed
Merge branch '4.2'
* 4.2: Added a note about PHP extension and Symfony Form types extension Update choice_label docs to match given examples Improved the Fragment Naming for Collections section [Form] Mention that form_theme _self only works with template inheritance Improved the explanation of the block_name form option [Diversity] Add Voting Logic Examples
2 parents 4f90e0e + 2fa596e commit fe6d763

File tree

5 files changed

+116
-20
lines changed

5 files changed

+116
-20
lines changed

contributing/diversity/governance.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ For an actionable item to pass, approval from greater than 50% of the voting
7272
guidance team members is required. Use or management of finances/donations
7373
require at least a two-thirds majority to pass.
7474

75+
For transparency and ease-of-understanding, this means only the following
76+
combinations of votes will result in an actionable item passing:
77+
78+
+-----+---------+---------+
79+
| For | Against | Abstain |
80+
+=====+=========+=========+
81+
| 5 | 0 | 0 |
82+
+-----+---------+---------+
83+
| 4 | 1 | 0 |
84+
+-----+---------+---------+
85+
| 3 | 2 | 0 |
86+
+-----+---------+---------+
87+
| 4 | 0 | 1 |
88+
+-----+---------+---------+
89+
| 3 | 1 | 1 |
90+
+-----+---------+---------+
91+
7592
Guidance Principles
7693
-------------------
7794

form/create_custom_field_type.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ Defining the Field Type
1616
In order to create the custom field type, first you have to create the class
1717
representing the field. In this situation the class holding the field type
1818
will be called ``ShippingType`` and the file will be stored in the default location
19-
for form fields, which is ``App\Form\Type``. Make sure the field extends
20-
:class:`Symfony\\Component\\Form\\AbstractType`::
19+
for form fields, which is ``App\Form\Type``.
20+
21+
All field types must implement the :class:`Symfony\\Component\\Form\\FormTypeInterface`,
22+
but you should instead extend from :class:`Symfony\\Component\\Form\\AbstractType`,
23+
which already implements that interface and provides some utilities::
2124

2225
// src/Form/Type/ShippingType.php
2326
namespace App\Form\Type;
@@ -53,8 +56,17 @@ for form fields, which is ``App\Form\Type``. Make sure the field extends
5356
Here, the return value of the ``getParent()`` function indicates that you're
5457
extending the ``ChoiceType`` field. This means that, by default, you inherit
5558
all of the logic and rendering of that field type. To see some of the logic,
56-
check out the `ChoiceType`_ class. There are three methods that are particularly
57-
important:
59+
check out the `ChoiceType`_ class.
60+
61+
.. note::
62+
63+
The PHP class extension mechanism and the Symfony form field extension
64+
mechanism are not the same. The parent type returned in ``getParent()`` is
65+
what Symfony uses to build and manage the field type. Making the PHP class
66+
extend from ``AbstractType`` is only a convenience way of implementing the
67+
required ``FormTypeInterface``.
68+
69+
There are three methods that are particularly important:
5870

5971
.. _form-type-methods-explanation:
6072

form/form_themes.rst

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,66 @@ Fragment Naming for Collections
307307
...............................
308308

309309
When using a :doc:`collection of forms </form/form_collections>`, the fragment
310-
of each collection item follows the pattern ``_field-name_entry_part``. For
311-
example, if your form field is named ``tasks``, the fragment for each task will
312-
be named ``_tasks_entry`` (``_tasks_entry_row``, ``_tasks_entry_label``,
313-
``_tasks_entry_widget``, ``_tasks_entry_error``)
310+
of each collection item follows a predefined pattern. For example, consider the
311+
following complex example where a ``TaskManagerType`` has a collection of
312+
``TaskListType`` which in turn has a collection of ``TaskType``::
313+
314+
class TaskManagerType extends AbstractType
315+
{
316+
public function buildForm(FormBuilderInterface $builder, array $options = array())
317+
{
318+
// ...
319+
$builder->add('taskLists', CollectionType::class, array(
320+
'entry_type' => TaskListType::class,
321+
'block_name' => 'task_lists',
322+
));
323+
}
324+
}
325+
326+
class TaskListType extends AbstractType
327+
{
328+
public function buildForm(FormBuilderInterface $builder, array $options = array())
329+
{
330+
// ...
331+
$builder->add('tasks', CollectionType::class, array(
332+
'entry_type' => TaskType::class,
333+
));
334+
}
335+
}
336+
337+
class TaskType
338+
{
339+
public function buildForm(FormBuilderInterface $builder, array $options = array())
340+
{
341+
$builder->add('name');
342+
// ...
343+
}
344+
}
345+
346+
Then you get all the following customizable blocks (where ``*`` can be replaced
347+
by ``row``, ``widget``, ``label``, or ``help``):
348+
349+
.. code-block:: twig
350+
351+
{% block _task_manager_task_lists_* %}
352+
{# the collection field of TaskManager #}
353+
{% endblock %}
354+
355+
{% block _task_manager_task_lists_entry_* %}
356+
{# the inner TaskListType #}
357+
{% endblock %}
358+
359+
{% block _task_manager_task_lists_entry_tasks_* %}
360+
{# the collection field of TaskListType #}
361+
{% endblock %}
362+
363+
{% block _task_manager_task_lists_entry_tasks_entry_* %}
364+
{# the inner TaskType #}
365+
{% endblock %}
366+
367+
{% block _task_manager_task_lists_entry_tasks_entry_name_* %}
368+
{# the field of TaskType #}
369+
{% endblock %}
314370
315371
Template Fragment Inheritance
316372
.............................
@@ -341,6 +397,8 @@ for any overridden form blocks:
341397

342398
.. code-block:: html+twig
343399

400+
{% extends 'base.html.twig' %}
401+
344402
{% form_theme form _self %}
345403

346404
{# this overrides the widget of any field of type integer, but only in the
@@ -359,12 +417,16 @@ for any overridden form blocks:
359417
</div>
360418
{% endblock %}
361419

362-
363420
{# ... render the form ... #}
364421

365-
The disadvantage of this method is that the customized form blocks can't be
366-
reused when rendering other forms in other templates. If that's what you need,
367-
create a form theme in a separate template as explained in the next section.
422+
The main disadvantage of this method is that it only works if your template
423+
extends another (``'base.html.twig'`` in the previous example). If your template
424+
does not, you must point ``form_theme`` to a separate template, as explained in
425+
the next section.
426+
427+
Another disadvantage is that the customized form blocks can't be reused when
428+
rendering other forms in other templates. If that's what you need, create a form
429+
theme in a separate template as explained in the next section.
368430

369431
Creating a Form Theme in a Separate Template
370432
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

reference/forms/types/options/block_name.rst.inc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ block_name
44
**type**: ``string`` **default**: the form's name (see :ref:`Knowing which
55
block to customize <form-customization-sidebar>`)
66
7-
Allows you to override the block name used to render the form type.
8-
Useful for example if you have multiple instances of the same form and you
9-
need to personalize the rendering of the forms individually.
7+
Allows you to add a custom block name to the ones used by default to render the
8+
form type. Useful for example if you have multiple instances of the same form
9+
and you need to personalize the rendering of the forms individually.
10+
11+
If you set for example this option to ``my_custom_name`` and the field is of
12+
type ``text``, Symfony will use the following names (and in this order) to find
13+
the block used to render the widget of the field: ``_my_custom_name_widget``,
14+
``text_widget`` and ``form_widget``.

reference/forms/types/options/choice_label.rst.inc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ more control::
1616
'no' => false,
1717
'maybe' => null,
1818
],
19-
'choice_label' => function ($choiceValue, $key, $value) {
20-
if ($value == $choiceValue) {
19+
'choice_label' => function ($value, $key, $choiceValue) {
20+
if (true === $value) {
2121
return 'Definitely!';
2222
}
2323

@@ -28,9 +28,9 @@ more control::
2828
},
2929
]);
3030

31-
This method is called for *each* choice, passing you the choice ``$value`` and the
32-
``$key`` from the choices array (``$index`` is related to `choice_value`_). This
33-
will give you:
31+
This method is called for *each* choice, passing you the ``$value`` and
32+
``$key`` from the choices array (additional ``$choiceValue`` is related to `choice_value`_).
33+
This will give you:
3434

3535
.. image:: /_images/reference/form/choice-example2.png
3636
:align: center

0 commit comments

Comments
 (0)