@@ -192,11 +192,11 @@ this folder.
192
192
just input ``text `` fields, you should customize the ``text_errors `` fragment.
193
193
194
194
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 ``
196
196
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
198
198
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 ``).
200
200
201
201
For more information on this topic, see :ref: `form-template-blocks `.
202
202
@@ -694,40 +694,55 @@ By default, the errors are rendered inside an unordered list:
694
694
</ul >
695
695
696
696
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.
698
698
699
699
.. configuration-block ::
700
700
701
701
.. code-block :: html+jinja
702
702
703
- {# fields_errors .html.twig #}
704
- {% block field_errors %}
703
+ {# form_errors .html.twig #}
704
+ {% block form_errors %}
705
705
{% spaceless %}
706
706
{% if errors|length > 0 %}
707
707
<ul class="error_list">
708
708
{% 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>
710
714
{% endfor %}
711
715
</ul>
712
716
{% endif %}
713
717
{% endspaceless %}
714
- {% endblock field_errors %}
718
+ {% endblock form_errors %}
715
719
716
720
.. code-block :: html+php
717
721
718
- <!-- fields_errors .html.php -->
722
+ <!-- form_errors .html.php -->
719
723
<?php if ($errors): ?>
720
724
<ul class="error_list">
721
725
<?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>
727
741
<?php endforeach; ?>
728
742
</ul>
729
743
<?php endif ?>
730
744
745
+
731
746
.. tip ::
732
747
See :ref: `cookbook-form-theming-methods ` for how to apply this customization.
733
748
@@ -748,33 +763,33 @@ to just one field) are rendered separately, usually at the top of your form:
748
763
To customize *only * the markup used for these errors, follow the same directions
749
764
as above, but now call the block ``form_errors `` (Twig) / the file ``form_errors.html.php ``
750
765
(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 ``.
752
767
753
768
Customizing the "Form Row"
754
769
~~~~~~~~~~~~~~~~~~~~~~~~~~
755
770
756
771
When you can manage it, the easiest way to render a form field is via the
757
772
``form_row `` function, which renders the label, errors and HTML widget of
758
773
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
760
775
class to the ``div `` element around each row:
761
776
762
777
.. configuration-block ::
763
778
764
779
.. code-block :: html+jinja
765
780
766
- {# field_row .html.twig #}
767
- {% block field_row %}
781
+ {# form_row .html.twig #}
782
+ {% block form_row %}
768
783
<div class="form_row">
769
784
{{ form_label(form) }}
770
785
{{ form_errors(form) }}
771
786
{{ form_widget(form) }}
772
787
</div>
773
- {% endblock field_row %}
788
+ {% endblock form_row %}
774
789
775
790
.. code-block :: html+php
776
791
777
- <!-- field_row .html.php -->
792
+ <!-- form_row .html.php -->
778
793
<div class="form_row">
779
794
<?php echo $view['form']->label($form) ?>
780
795
<?php echo $view['form']->errors($form) ?>
@@ -788,17 +803,17 @@ Adding a "Required" Asterisk to Field Labels
788
803
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
789
804
790
805
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.
792
807
793
808
In Twig, if you're making the form customization inside the same template as your
794
809
form, modify the ``use `` tag and add the following:
795
810
796
811
.. code-block :: html+jinja
797
812
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 %}
799
814
800
- {% block field_label %}
801
- {{ block('base_field_label ') }}
815
+ {% block form_label %}
816
+ {{ block('base_form_label ') }}
802
817
803
818
{% if required %}
804
819
<span class="required" title="This field is required">*</span>
@@ -812,7 +827,7 @@ the following:
812
827
813
828
{% extends 'form_div_layout.html.twig' %}
814
829
815
- {% block field_label %}
830
+ {% block form_label %}
816
831
{{ parent() }}
817
832
818
833
{% if required %}
@@ -825,10 +840,13 @@ original template:
825
840
826
841
.. code-block :: html+php
827
842
828
- <!-- field_label .html.php -->
843
+ <!-- form_label .html.php -->
829
844
830
845
<!-- 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>
832
850
833
851
<!-- customization -->
834
852
<?php if ($required) : ?>
0 commit comments