Skip to content

Commit 49f2b2e

Browse files
committed
new translation_parameters form option
1 parent 3927279 commit 49f2b2e

15 files changed

+402
-59
lines changed

reference/forms/twig_reference.rst

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,14 @@ Look at the ``form_label`` as an example:
269269
{{ attrname }}="{{ attrvalue }}"
270270
{%- endfor %}
271271
>
272-
{{ label|trans({}, translation_domain) }}
272+
{{ label|trans(translation_parameters, translation_domain) }}
273273
</label>
274274
{% endblock form_label %}
275275
276276
This block makes use of several variables: ``compound``, ``label_attr``,
277-
``required``, ``label``, ``name`` and ``translation_domain``. These variables
278-
are made available by the form rendering system. But more importantly, these
279-
are the variables that you can override when calling ``form_label()`` (since
277+
``required``, ``label``, ``name``, ``translation_parameters`` and ``translation_domain``.
278+
These variables are made available by the form rendering system. But more importantly,
279+
these are the variables that you can override when calling ``form_label()`` (since
280280
in this example, you're rendering the label).
281281

282282
The exact variables available to override depends on which part of the form
@@ -326,59 +326,61 @@ done by using a public ``vars`` property on the
326326
{{ form.name.vars.label }}
327327
</label>
328328

329-
+------------------------+-------------------------------------------------------------------------------------+
330-
| Variable | Usage |
331-
+========================+=====================================================================================+
332-
| ``form`` | The current ``FormView`` instance. |
333-
+------------------------+-------------------------------------------------------------------------------------+
334-
| ``id`` | The ``id`` HTML attribute to be rendered. |
335-
+------------------------+-------------------------------------------------------------------------------------+
336-
| ``name`` | The name of the field (e.g. ``title``) - but not the ``name`` |
337-
| | HTML attribute, which is ``full_name``. |
338-
+------------------------+-------------------------------------------------------------------------------------+
339-
| ``full_name`` | The ``name`` HTML attribute to be rendered. |
340-
+------------------------+-------------------------------------------------------------------------------------+
341-
| ``errors`` | An array of any errors attached to *this* specific field |
342-
| | (e.g. ``form.title.errors``). |
343-
| | Note that you can't use ``form.errors`` to determine if a form is valid, |
344-
| | since this only returns "global" errors: some individual fields may have errors. |
345-
| | Instead, use the ``valid`` option. |
346-
+------------------------+-------------------------------------------------------------------------------------+
347-
| ``submitted`` | Returns ``true`` or ``false`` depending on whether the whole form is submitted |
348-
+------------------------+-------------------------------------------------------------------------------------+
349-
| ``valid`` | Returns ``true`` or ``false`` depending on whether the whole form is valid. |
350-
+------------------------+-------------------------------------------------------------------------------------+
351-
| ``value`` | The value that will be used when rendering (commonly the ``value`` HTML attribute). |
352-
+------------------------+-------------------------------------------------------------------------------------+
353-
| ``disabled`` | If ``true``, ``disabled="disabled"`` is added to the field. |
354-
+------------------------+-------------------------------------------------------------------------------------+
355-
| ``required`` | If ``true``, a ``required`` attribute is added to the field to activate HTML5 |
356-
| | validation. Additionally, a ``required`` class is added to the label. |
357-
+------------------------+-------------------------------------------------------------------------------------+
358-
| ``label`` | The string label that will be rendered. |
359-
+------------------------+-------------------------------------------------------------------------------------+
360-
| ``multipart`` | If ``true``, ``form_enctype`` will render ``enctype="multipart/form-data"``. |
361-
| | This only applies to the root form element. |
362-
+------------------------+-------------------------------------------------------------------------------------+
363-
| ``attr`` | A key-value array that will be rendered as HTML attributes on the field. |
364-
+------------------------+-------------------------------------------------------------------------------------+
365-
| ``label_attr`` | A key-value array that will be rendered as HTML attributes on the label. |
366-
+------------------------+-------------------------------------------------------------------------------------+
367-
| ``compound`` | Whether or not a field is actually a holder for a group of children fields |
368-
| | (for example, a ``choice`` field, which is actually a group of checkboxes. |
369-
+------------------------+-------------------------------------------------------------------------------------+
370-
| ``block_prefixes`` | An array of all the names of the parent types. |
371-
+------------------------+-------------------------------------------------------------------------------------+
372-
| ``translation_domain`` | The domain of the translations for this form. |
373-
+------------------------+-------------------------------------------------------------------------------------+
374-
| ``cache_key`` | A unique key which is used for caching. |
375-
+------------------------+-------------------------------------------------------------------------------------+
376-
| ``data`` | The normalized data of the type. |
377-
+------------------------+-------------------------------------------------------------------------------------+
378-
| ``method`` | The method of the current form (POST, GET, etc.). |
379-
+------------------------+-------------------------------------------------------------------------------------+
380-
| ``action`` | The action of the current form. |
381-
+------------------------+-------------------------------------------------------------------------------------+
329+
+----------------------------+-------------------------------------------------------------------------------------+
330+
| Variable | Usage |
331+
+============================+=====================================================================================+
332+
| ``form`` | The current ``FormView`` instance. |
333+
+----------------------------+-------------------------------------------------------------------------------------+
334+
| ``id`` | The ``id`` HTML attribute to be rendered. |
335+
+----------------------------+-------------------------------------------------------------------------------------+
336+
| ``name`` | The name of the field (e.g. ``title``) - but not the ``name`` |
337+
| | HTML attribute, which is ``full_name``. |
338+
+----------------------------+-------------------------------------------------------------------------------------+
339+
| ``full_name`` | The ``name`` HTML attribute to be rendered. |
340+
+----------------------------+-------------------------------------------------------------------------------------+
341+
| ``errors`` | An array of any errors attached to *this* specific field |
342+
| | (e.g. ``form.title.errors``). |
343+
| | Note that you can't use ``form.errors`` to determine if a form is valid, |
344+
| | since this only returns "global" errors: some individual fields may have errors. |
345+
| | Instead, use the ``valid`` option. |
346+
+----------------------------+-------------------------------------------------------------------------------------+
347+
| ``submitted`` | Returns ``true`` or ``false`` depending on whether the whole form is submitted |
348+
+----------------------------+-------------------------------------------------------------------------------------+
349+
| ``valid`` | Returns ``true`` or ``false`` depending on whether the whole form is valid. |
350+
+----------------------------+-------------------------------------------------------------------------------------+
351+
| ``value`` | The value that will be used when rendering (commonly the ``value`` HTML attribute). |
352+
+----------------------------+-------------------------------------------------------------------------------------+
353+
| ``disabled`` | If ``true``, ``disabled="disabled"`` is added to the field. |
354+
+----------------------------+-------------------------------------------------------------------------------------+
355+
| ``required`` | If ``true``, a ``required`` attribute is added to the field to activate HTML5 |
356+
| | validation. Additionally, a ``required`` class is added to the label. |
357+
+----------------------------+-------------------------------------------------------------------------------------+
358+
| ``label`` | The string label that will be rendered. |
359+
+----------------------------+-------------------------------------------------------------------------------------+
360+
| ``multipart`` | If ``true``, ``form_enctype`` will render ``enctype="multipart/form-data"``. |
361+
| | This only applies to the root form element. |
362+
+----------------------------+-------------------------------------------------------------------------------------+
363+
| ``attr`` | A key-value array that will be rendered as HTML attributes on the field. |
364+
+----------------------------+-------------------------------------------------------------------------------------+
365+
| ``label_attr`` | A key-value array that will be rendered as HTML attributes on the label. |
366+
+----------------------------+-------------------------------------------------------------------------------------+
367+
| ``compound`` | Whether or not a field is actually a holder for a group of children fields |
368+
| | (for example, a ``choice`` field, which is actually a group of checkboxes. |
369+
+----------------------------+-------------------------------------------------------------------------------------+
370+
| ``block_prefixes`` | An array of all the names of the parent types. |
371+
+----------------------------+-------------------------------------------------------------------------------------+
372+
| ``translation_domain`` | The domain of the translations for this form. |
373+
+----------------------------+-------------------------------------------------------------------------------------+
374+
| ``translation_parameters`` | An array of translation parameters for this form. |
375+
+----------------------------+-------------------------------------------------------------------------------------+
376+
| ``cache_key`` | A unique key which is used for caching. |
377+
+----------------------------+-------------------------------------------------------------------------------------+
378+
| ``data`` | The normalized data of the type. |
379+
+----------------------------+-------------------------------------------------------------------------------------+
380+
| ``method`` | The method of the current form (POST, GET, etc.). |
381+
+----------------------------+-------------------------------------------------------------------------------------+
382+
| ``action`` | The action of the current form. |
383+
+----------------------------+-------------------------------------------------------------------------------------+
382384

383385
.. _`form_div_layout.html.twig`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
384386
.. _`the Twig documentation`: https://twig.symfony.com/doc/2.x/templates.html#test-operator

reference/forms/types/button.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ A simple, non-responsive button.
1313
| options | - `disabled`_ |
1414
| | - `label`_ |
1515
| | - `translation_domain`_ |
16+
| | - `translation_parameters`_ |
1617
+----------------------+----------------------------------------------------------------------+
1718
| Parent type | none |
1819
+----------------------+----------------------------------------------------------------------+
@@ -49,3 +50,5 @@ as a key. This can be useful when you need to set a custom class for the button:
4950
.. include:: /reference/forms/types/options/button_label.rst.inc
5051

5152
.. include:: /reference/forms/types/options/button_translation_domain.rst.inc
53+
54+
.. include:: /reference/forms/types/options/button_translation_parameters.rst.inc

reference/forms/types/choice.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ To use this field, you must specify *either* ``choices`` or ``choice_loader`` op
4343
| | - `mapped`_ |
4444
| | - `required`_ |
4545
| | - `translation_domain`_ |
46+
| | - `translation_parameters`_ |
4647
+-------------+------------------------------------------------------------------------------+
4748
| Parent type | :doc:`FormType </reference/forms/types/form>` |
4849
+-------------+------------------------------------------------------------------------------+
@@ -283,6 +284,8 @@ These options inherit from the :doc:`FormType </reference/forms/types/form>`:
283284

284285
.. include:: /reference/forms/types/options/choice_type_translation_domain.rst.inc
285286

287+
.. include:: /reference/forms/types/options/choice_type_translation_parameters.rst.inc
288+
286289
Field Variables
287290
---------------
288291

reference/forms/types/entity.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ objects from the database.
3232
| | - `placeholder`_ |
3333
| | - `preferred_choices`_ |
3434
| | - `translation_domain`_ |
35+
| | - `translation_parameters`_ |
3536
| | - `trim`_ |
3637
| | |
3738
| | from the :doc:`FormType </reference/forms/types/form>`: |
@@ -282,6 +283,8 @@ These options inherit from the :doc:`ChoiceType </reference/forms/types/choice>`
282283

283284
.. include:: /reference/forms/types/options/choice_type_translation_domain.rst.inc
284285

286+
.. include:: /reference/forms/types/options/entity_type_translation_parameters.rst.inc
287+
285288
.. include:: /reference/forms/types/options/choice_type_trim.rst.inc
286289

287290
These options inherit from the :doc:`form </reference/forms/types/form>`

reference/forms/types/form.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ on all types for which ``FormType`` is the parent.
3838
| | - `disabled`_ |
3939
| | - `label`_ |
4040
| | - `translation_domain`_ |
41+
| | - `translation_parameters`_ |
4142
+-----------+--------------------------------------------------------------------+
4243
| Parent | none |
4344
+-----------+--------------------------------------------------------------------+
@@ -151,3 +152,5 @@ of the form type tree (i.e. it cannot be used as a form type on its own).
151152
.. include:: /reference/forms/types/options/label.rst.inc
152153

153154
.. include:: /reference/forms/types/options/translation_domain.rst.inc
155+
156+
.. include:: /reference/forms/types/options/translation_parameters.rst.inc
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
translation_parameters
2+
~~~~~~~~~~~~~~~~~~~~~~
3+
4+
**type**: ``array`` **default**: ``array()``
5+
6+
Translated `label`_ can contain
7+
:ref:`placeholders <component-translation-placeholders>`.
8+
This option allows you to pass an array of parameters in order to replace
9+
placeholders with actual values.
10+
11+
Given this translation message:
12+
13+
.. code-block:: yaml
14+
15+
# translations/messages.en.yml
16+
form.order_list.submit_to_company: Send an order to %company%
17+
18+
you can specify placeholder value:
19+
20+
.. code-block:: php
21+
22+
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
23+
// ...
24+
25+
$builder->add('send', ButtonType::class, array(
26+
'label' => 'form.order_list.submit_to_company',
27+
'translation_parameters' => array(
28+
'%company%' => 'ACME Inc.'
29+
)
30+
));
31+
32+
Note that `translation_parameters` of buttons are merged with those of parent.
33+
In other words the parent's parameters are available for buttons but can be
34+
overriden:
35+
36+
.. code-block:: php
37+
38+
// App\Controller\OrderListController
39+
40+
$form = $this->createForm(OrderListType::class, null, array(
41+
'translation_parameters' => array(
42+
'%company%' => 'ACME Inc.'
43+
)
44+
));
45+
46+
.. code-block:: php
47+
48+
// App\Form\OrderListType
49+
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
50+
// ...
51+
52+
$builder->add('send', ButtonType::class, array(
53+
'label' => 'form.order_list.submit_to_company',
54+
));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ translation_domain
55

66
In case `choice_translation_domain`_ is set to ``true`` or ``null``, this
77
configures the exact translation domain that will be used for any labels or
8-
options that are rendered for this field
8+
options that are rendered for this field.

0 commit comments

Comments
 (0)