8000 Documenting the block_prefix option · symfony/symfony-docs@417885e · GitHub
[go: up one dir, main page]

Skip to content

Commit 417885e

Browse files
committed
Documenting the block_prefix option
1 parent 3f20558 commit 417885e

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

form/create_custom_field_type.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ The goal of this field was to extend the choice type to enable selection of the
8787
shipping type. This is achieved by fixing the ``choices`` to a list of available
8888
shipping options.
8989

90+
.. tip::
91+
92+
If the purpose of this new form type was to customize the rendering of some
93+
fields only, skip this step and use ``block_name`` or ``block_prefix`` option
94+
instead. For more information, see :ref:`form-customization-form-themes`.
95+
9096
Creating a Template for the Field
9197
---------------------------------
9298

form/form_customization.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,35 @@ class to the ``div`` element around each row:
735735
</div>
736736
{% endblock form_row %}
737737

738+
.. tip::
739+
740+
If you want to customize some instances of the same form only (without
741+
the need to create a new form type) you can set the ``block_prefix``
742+
option in your form type::
743+
744+
use Symfony\Component\Form\Extension\Core\Type\TextType;
745+
use Symfony\Component\Form\FormBuilderInterface;
746+
747+
public function buildForm(FormBuilderInterface $builder, array $options)
748+
{
749+
$builder->add('name', TextType::class, array(
750+
'block_prefix' => 'wrapped_text',
751+
));
752+
}
753+
754+
.. versionadded:: 4.3
755+
The ``block_prefix`` option was introduced in Symfony 4.3.
756+
757+
Then the block name will be ``wrapped_text_row``.
758+
759+
.. code-block:: html+twig
760+
761+
{% block wrapped_text_row %}
762+
<div class="wrapped">
763+
{{ form_row(form) }}
764+
</div>
765+
{% endblock wrapped_text_row %}
766+
738767
.. tip::
739768

740769
See :ref:`form-theming-methods` for how to apply this customization.

reference/forms/types/form.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ on all types for which ``FormType`` is the parent.
3737
| Inherited | - `attr`_ |
3838
| options | - `auto_initialize`_ |
3939
| | - `block_name`_ |
40+
| | - `block_prefix`_ |
4041
| | - `disabled`_ |
4142
| | - `label`_ |
4243
| | - `translation_domain`_ |
@@ -154,6 +155,8 @@ of the form type tree (i.e. it cannot be used as a form type on its own).
154155

155156
.. include:: /reference/forms/types/options/block_name.rst.inc
156157

158+
.. include:: /reference/forms/types/options/block_prefix.rst.inc
159+
157160
.. include:: /reference/forms/types/options/disabled.rst.inc
158161

159162
.. include:: /reference/forms/types/options/label.rst.inc
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
block_prefix
2+
~~~~~~~~~~~~
3+
4+
**type**: ``string`` or ``null`` **default**: ``null`` (see :ref:`Knowing which
5+
block to customize <form-customization-sidebar>`)
6+
7+
.. versionadded:: 4.3
8+
The ``block_prefix`` option was introduced in Symfony 4.3.
9+
10+
Allows you to add a custom block prefix and override the block name
11+
used to render the form type. Useful for example if you have multiple
12+
instances of the same form and you need to personalize the rendering
13+
of all of them without the need to create a new form type.

0 commit comments

Comments
 (0)
0