Skip to content

Commit 4775365

Browse files
committed
[Form][#1738] Updating more form blocks for 2.1
1 parent 7ee31a2 commit 4775365

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

book/forms.rst

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,27 +1111,27 @@ do this, create a new template file that will store the new markup:
11111111
.. code-block:: html+jinja
11121112

11131113
{# src/Acme/TaskBundle/Resources/views/Form/fields.html.twig #}
1114-
{% block field_row %}
1114+
{% block form_row %}
11151115
{% spaceless %}
11161116
<div class="form_row">
11171117
{{ form_label(form) }}
11181118
{{ form_errors(form) }}
11191119
{{ form_widget(form) }}
11201120
</div>
11211121
{% endspaceless %}
1122-
{% endblock field_row %}
1122+
{% endblock form_row %}
11231123

11241124
.. code-block:: html+php
11251125

1126-
<!-- src/Acme/TaskBundle/Resources/views/Form/field_row.html.php -->
1126+
<!-- src/Acme/TaskBundle/Resources/views/Form/form_row.html.php -->
11271127
<div class="form_row">
11281128
<?php echo $view['form']->label($form, $label) ?>
11291129
<?php echo $view['form']->errors($form) ?>
11301130
<?php echo $view['form']->widget($form, $parameters) ?>
11311131
</div>
11321132

1133-
The ``field_row`` form fragment is used when rendering most fields via the
1134-
``form_row`` function. To tell the form component to use your new ``field_row``
1133+
The ``form_row`` form fragment is used when rendering most fields via the
1134+
``form_row`` function. To tell the form component to use your new ``form_row``
11351135
fragment defined above, add the following to the top of the template that
11361136
renders the form:
11371137

@@ -1157,8 +1157,8 @@ renders the form:
11571157

11581158
The ``form_theme`` tag (in Twig) "imports" the fragments defined in the given
11591159
template and uses them when rendering the form. In other words, when the
1160-
``form_row`` function is called later in this template, it will use the ``field_row``
1161-
block from your custom theme (instead of the default ``field_row`` block
1160+
``form_row`` function is called later in this template, it will use the ``form_row``
1161+
block from your custom theme (instead of the default ``form_row`` block
11621162
that ships with Symfony).
11631163

11641164
Your custom theme does not have to override all the blocks. When rendering a block
@@ -1209,10 +1209,10 @@ the `Resources/views/Form` directory of the framework bundle (`view on GitHub`_)
12091209
Each fragment name follows the same basic pattern and is broken up into two pieces,
12101210
separated by a single underscore character (``_``). A few examples are:
12111211

1212-
* ``field_row`` - used by ``form_row`` to render most fields;
1212+
* ``form_row`` - used by ``form_row`` to render most fields;
12131213
* ``textarea_widget`` - used by ``form_widget`` to render a ``textarea`` field
12141214
type;
1215-
* ``field_errors`` - used by ``form_errors`` to render errors for a field;
1215+
* ``form_errors`` - used by ``form_errors`` to render errors for a field;
12161216

12171217
Each fragment follows the same basic pattern: ``type_part``. The ``type`` portion
12181218
corresponds to the field *type* being rendered (e.g. ``textarea``, ``checkbox``,
@@ -1249,16 +1249,17 @@ In some cases, the fragment you want to customize will appear to be missing.
12491249
For example, there is no ``textarea_errors`` fragment in the default themes
12501250
provided with Symfony. So how are the errors for a textarea field rendered?
12511251

1252-
The answer is: via the ``field_errors`` fragment. When Symfony renders the errors
1252+
The answer is: via the ``form_errors`` fragment. When Symfony renders the errors
12531253
for a textarea type, it looks first for a ``textarea_errors`` fragment before
1254-
falling back to the ``field_errors`` fragment. Each field type has a *parent*
1255-
type (the parent type of ``textarea`` is ``field``), and Symfony uses the
1256-
fragment for the parent type if the base fragment doesn't exist.
1254+
falling back to the ``form_errors`` fragment. Each field type has a *parent*
1255+
type (the parent type of ``textarea`` is ``text``, its parent is ``form``),
1256+
and Symfony uses the fragment for the parent type if the base fragment doesn't
1257+
exist.
12571258

12581259
So, to override the errors for *only* ``textarea`` fields, copy the
1259-
``field_errors`` fragment, rename it to ``textarea_errors`` and customize it. To
1260+
``form_errors`` fragment, rename it to ``textarea_errors`` and customize it. To
12601261
override the default error rendering for *all* fields, copy and customize the
1261-
``field_errors`` fragment directly.
1262+
``form_errors`` fragment directly.
12621263

12631264
.. tip::
12641265

@@ -1329,9 +1330,9 @@ to define form output.
13291330
{% form_theme form _self %}
13301331

13311332
{# make the form fragment customization #}
1332-
{% block field_row %}
1333+
{% block form_row %}
13331334
{# custom field row output #}
1334-
{% endblock field_row %}
1335+
{% endblock form_row %}
13351336

13361337
{% block content %}
13371338
{# ... #}

cookbook/form/form_customization.rst

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ this folder.
192192
just input ``text`` fields, you should customize the ``text_errors`` fragment.
193193

194194
More commonly, however, you'll want to customize how errors are displayed
195-
across *all* fields. You can do this by customizing the ``field_errors``
195+
across *all* fields. You can do this by customizing the ``form_errors``
196196
fragment. This takes advantage of field type inheritance. Specifically,
197-
since the ``text`` type extends from the ``field`` type, the form component
197+
since the ``text`` type extends from the ``form`` type, the form component
198198
will first look for the type-specific fragment (e.g. ``text_errors``) before
199-
falling back to its parent fragment name if it doesn't exist (e.g. ``field_errors``).
199+
falling back to its parent fragment name if it doesn't exist (e.g. ``form_errors``).
200200

201201
For more information on this topic, see :ref:`form-template-blocks`.
202202

@@ -694,40 +694,55 @@ By default, the errors are rendered inside an unordered list:
694694
</ul>
695695

696696
To override how errors are rendered for *all* fields, simply copy, paste
697-
and customize the ``field_errors`` fragment.
697+
and customize the ``form_errors`` fragment.
698698

699699
.. configuration-block::
700700

701701
.. code-block:: html+jinja
702702

703-
{# fields_errors.html.twig #}
704-
{% block field_errors %}
703+
{# form_errors.html.twig #}
704+
{% block form_errors %}
705705
{% spaceless %}
706706
{% if errors|length > 0 %}
707707
<ul class="error_list">
708708
{% for error in errors %}
709-
<li>{{ error.messageTemplate|trans(error.messageParameters, 'validators') }}</li>
709+
<li>{{
710+
error.messagePluralization is null
711+
? error.messageTemplate|trans(error.messageParameters, 'validators')
712+
: error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
713+
}}</li>
710714
{% endfor %}
711715
</ul>
712716
{% endif %}
713717
{% endspaceless %}
714-
{% endblock field_errors %}
718+
{% endblock form_errors %}
715719

716720
.. code-block:: html+php
717721

718-
<!-- fields_errors.html.php -->
722+
<!-- form_errors.html.php -->
719723
<?php if ($errors): ?>
720724
<ul class="error_list">
721725
<?php foreach ($errors as $error): ?>
722-
<li><?php echo $view['translator']->trans(
723-
$error->getMessageTemplate(),
724-
$error->getMessageParameters(),
725-
'validators'
726-
) ?></li>
726+
<li><?php
727+
if (null === $error->getMessagePluralization()) {
728+
echo $view['translator']->trans(
729+
$error->getMessageTemplate(),
730+
$error->getMessageParameters(),
731+
'validators'
732+
);
733+
} else {
734+
echo $view['translator']->transChoice(
735+
$error->getMessageTemplate(),
736+
$error->getMessagePluralization(),
737+
$error->getMessageParameters(),
738+
'validators'
739+
);
740+
}?></li>
727741
<?php endforeach; ?>
728742
</ul>
729743
<?php endif ?>
730744

745+
731746
.. tip::
732747
See :ref:`cookbook-form-theming-methods` for how to apply this customization.
733748

@@ -748,33 +763,33 @@ to just one field) are rendered separately, usually at the top of your form:
748763
To customize *only* the markup used for these errors, follow the same directions
749764
as above, but now call the block ``form_errors`` (Twig) / the file ``form_errors.html.php``
750765
(PHP). Now, when errors for the ``form`` type are rendered, your customized
751-
fragment will be used instead of the default ``field_errors``.
766+
fragment will be used instead of the default ``form_errors``.
752767

753768
Customizing the "Form Row"
754769
~~~~~~~~~~~~~~~~~~~~~~~~~~
755770

756771
When you can manage it, the easiest way to render a form field is via the
757772
``form_row`` function, which renders the label, errors and HTML widget of
758773
a field. To customize the markup used for rendering *all* form field rows,
759-
override the ``field_row`` fragment. For example, suppose you want to add a
774+
override the ``form_row`` fragment. For example, suppose you want to add a
760775
class to the ``div`` element around each row:
761776

762777
.. configuration-block::
763778

764779
.. code-block:: html+jinja
765780

766-
{# field_row.html.twig #}
767-
{% block field_row %}
781+
{# form_row.html.twig #}
782+
{% block form_row %}
768783
<div class="form_row">
769784
{{ form_label(form) }}
770785
{{ form_errors(form) }}
771786
{{ form_widget(form) }}
772787
</div>
773-
{% endblock field_row %}
788+
{% endblock form_row %}
774789

775790
.. code-block:: html+php
776791

777-
<!-- field_row.html.php -->
792+
<!-- form_row.html.php -->
778793
<div class="form_row">
779794
<?php echo $view['form']->label($form) ?>
780795
<?php echo $view['form']->errors($form) ?>
@@ -788,17 +803,17 @@ Adding a "Required" Asterisk to Field Labels
788803
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
789804

790805
If you want to denote all of your required fields with a required asterisk (``*``),
791-
you can do this by customizing the ``field_label`` fragment.
806+
you can do this by customizing the ``form_label`` fragment.
792807

793808
In Twig, if you're making the form customization inside the same template as your
794809
form, modify the ``use`` tag and add the following:
795810

796811
.. code-block:: html+jinja
797812

798-
{% use 'form_div_layout.html.twig' with field_label as base_field_label %}
813+
{% use 'form_div_layout.html.twig' with form_label as base_form_label %}
799814

800-
{% block field_label %}
801-
{{ block('base_field_label') }}
815+
{% block form_label %}
816+
{{ block('base_form_label') }}
802817

803818
{% if required %}
804819
<span class="required" title="This field is required">*</span>
@@ -812,7 +827,7 @@ the following:
812827

813828
{% extends 'form_div_layout.html.twig' %}
814829

815-
{% block field_label %}
830+
{% block form_label %}
816831
{{ parent() }}
817832

818833
{% if required %}
@@ -825,10 +840,13 @@ original template:
825840

826841
.. code-block:: html+php
827842

828-
<!-- field_label.html.php -->
843+
<!-- form_label.html.php -->
829844

830845
<!-- original content -->
831-
<label for="<?php echo $view->escape($id) ?>" <?php foreach($attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>><?php echo $view->escape($view['translator']->trans($label)) ?></label>
846+
<?php if ($required) { $label_attr['class'] = trim((isset($label_attr['class']) ? $label_attr['class'] : '').' required'); } ?>
847+
<?php if (!$compound) { $label_attr['for'] = $id; } ?>
848+
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
849+
<label <?php foreach ($label_attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>><?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></label>
832850

833851
<!-- customization -->
834852
<?php if ($required) : ?>

0 commit comments

Comments
 (0)