8000 feature #26332 Add a data_help method in Form (mpiot, Nyholm) · symfony/symfony@9dd89e0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9dd89e0

Browse files
committed
feature #26332 Add a data_help method in Form (mpiot, Nyholm)
This PR was merged into the 4.1-dev branch. Discussion ---------- Add a data_help method in Form | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26331 | License | MIT | Doc PR | symfony/symfony-docs#9361 Add a form_help method in twig to display a help message in form. A `help` keyword is added to all FormType to define the message. Commits ------- 585ca28 Add return type hint 859ee03 Revert: remove comment line from twig templates d723756 Fix some mistakes c74e0dc Use spaceless balises in Twig templates 8b937ff Try without try/catch 32bf1f6 Test the renderHelp method in all Tests about help to skip them if necessary. 437b77e Skip renderHelp test as skipped if not override d84be70 Update composer files 075fcfd [FrameworkBundle] Add widgetAtt to formTable/form_row f1d13a8 Fix Fabpot.io 69ded67 Added form_help on horizontal design and removed special variable fd53bc5 Enable aria-described in row for all Templates 98065d3 fabpot.io fix edb95f8 Use array long syntax aada72c Set help option on nul as default f948147 Rename help id (snake_case) 77fa317 Fix Test 30deaa9 PSR fix bf4d08c Add aria-describedBy on input 1f3a15e Rename id 058489d Add an id to the help 6ea7a20 Remove vars option from form_help ba798df FrameworkBundle Tests 4f2581d Use array long syntax f15bc79 Fix coding standards c934e49 Add test without help set 8094804 Add Tests 067c681 Template for table, Foundation and Bootstrap 3 d3e3e49 Fix: check translation domain 2c2c045 Adapt existant tests 831693a Add trans filter e311838 Remove raw filter for help 8b97c1b Use a shortcut to acces help var in Twig template 1b89f9d Add a template fot div_layout c8914f5 Add a data_help method in Form
2 parents d329a7a + 585ca28 commit 9dd89e0

32 files changed

+273
-23
lines changed

src/Symfony/Bridge/Twig/Extension/FormExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function getFunctions()
4747
new TwigFunction('form_widget', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
4848
new TwigFunction('form_errors', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
4949
new TwigFunction('form_label', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
50+
new TwigFunction('form_help', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
5051
new TwigFunction('form_row', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
5152
new TwigFunction('form_rest', null, array('node_class' => 'Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', 'is_safe' => array('html'))),
5253
new TwigFunction('form', null, array('node_class' => 'Symfony\Bridge\Twig\Node\RenderBlockNode', 'is_safe' => array('html'))),

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_horizontal_layout.html.twig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ col-sm-2
2323
{# Rows #}
2424

2525
{% block form_row -%}
26+
{%- set widget_attr = {} -%}
27+
{%- if help is not empty -%}
28+
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
29+
{%- endif -%}
2630
<div class="form-group{% if (not compound or force_error|default(false)) and not valid %} has-error{% endif %}">
2731
{{- form_label(form) -}}
2832
<div class="{{ block('form_group_class') }}">
29-
{{- form_widget(form) -}}
33+
{{- form_widget(form, widget_attr) -}}
34+
{{- form_help(form) -}}
3035
{{- form_errors(form) -}}
3136
</div>
3237
{##}</div>

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,14 @@
8989
{# Rows #}
9090

9191
{% block form_row -%}
92+
{%- set widget_attr = {} -%}
93+
{%- if help is not empty -%}
94+
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
95+
{%- endif -%}
9296
<div class="form-group{% if (not compound or force_error|default(false)) and not valid %} has-error{% endif %}">
9397
{{- form_label(form) -}}
94-
{{- form_widget(form) -}}
98+
{{- form_widget(form, widget_attr) -}}
99+
{{- form_help(form) -}}
95100
{{- form_errors(form) -}}
96101
</div>
97102
{%- endblock form_row %}
@@ -149,3 +154,17 @@
149154
{% if form is not rootform %}</span>{% else %}</div>{% endif %}
150155
{%- endif %}
151156
{%- endblock form_errors %}
157+
158+
{# Help #}
159+
160+
{% block form_help -%}
161+
{%- if help is not empty -%}
162+
<span id="{{ id }}_help" class="help-block">
163+
{%- if translation_domain is same as(false) -%}
164+
{{- help -}}
165+
{%- else -%}
166+
{{- help|trans({}, translation_domain) -}}
167+
{%- endif -%}
168+
</span>
169+
{%- endif -%}
170+
{%- endblock form_help %}

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_horizontal_layout.html.twig

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,31 @@ col-sm-2
2424
{%- if expanded is defined and expanded -%}
2525
{{ block('fieldset_form_row') }}
2626
{%- else -%}
27+
{%- set widget_attr = {} -%}
28+
{%- if help is not empty -%}
29+
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
30+
{%- endif -%}
2731
<div class="form-group row{% if (not compound or force_error|default(false)) and not valid %} is-invalid{% endif %}">
2832
{{- form_label(form) -}}
2933
<div class="{{ block('form_group_class') }}">
30-
{{- form_widget(form) -}}
34+
{{- form_widget(form, widget_attr) -}}
35+
{{- form_help(form) -}}
3136
</div>
3237
{##}</div>
3338
{%- endif -%}
3439
{%- endblock form_row %}
3540

3641
{% block fieldset_form_row -%}
42+
{%- set widget_attr = {} -%}
43+
{%- if help is not empty -%}
44+
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
45+
{%- endif -%}
3746
<fieldset class="form-group">
3847
<div class="row{% if (not compound or force_error|default(false)) and not valid %} is-invalid{% endif %}">
3948
{{- form_label(form) -}}
4049
<div class="{{ block('form_group_class') }}">
41-
{{- form_widget(form) -}}
50+
{{- form_widget(form, widget_attr) -}}
51+
{{- form_help(form) -}}
4252
</div>
4353
</div>
4454
{##}</fieldset>

src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,14 @@
259259
{%- if compound is defined and compound -%}
260260
{%- set element = 'fieldset' -%}
261261
{%- endif -%}
262+
{%- set widget_attr = {} -%}
263+
{%- if help is not empty -%}
264+
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
265+
{%- endif -%}
262266
<{{ element|default('div') }} class="form-group">
263267
{{- form_label(form) -}}
264-
{{- form_widget(form) -}}
268+
{{- form_widget(form, widget_attr) -}}
269+
{{- form_help(form) -}}
265270
</{{ element|default('div') }}>
266271
{%- endblock form_row %}
267272

@@ -287,3 +292,17 @@
287292
</span>
288293
{%- endif %}
289294
{%- endblock form_errors %}
295+
296+
{# Help #}
297+
298+
{% block form_help -%}
299+
{%- if help is not empty -%}
300+
<small id="{{ id }}_help" class="form-text text-muted">
301+
{%- if translation_domain is same as(false) -%}
302+
{{- help -}}
303+
{%- else -%}
304+
{{- help|trans({}, translation_domain) -}}
305+
{%- endif -%}
306+
</small>
307+
{%- endif -%}
308+
{%- endblock form_help %}

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,20 @@
287287

288288
{%- block button_label -%}{%- endblock -%}
289289

290+
{# Help #}
291+
292+
{% block form_help -%}
293+
{%- if help is not empty -%}
294+
<p id="{{ id }}_help" class="help-text">
295+
{%- if translation_domain is same as(false) -%}
296+
{{- help -}}
297+
{%- else -%}
298+
{{- help|trans({}, translation_domain) -}}
299+
{%- endif -%}
300+
</p>
301+
{%- endif -%}
302+
{%- endblock form_help %}
303+
290304
{# Rows #}
291305

292306
{%- block repeated_row -%}
@@ -298,10 +312,15 @@
298312
{%- endblock repeated_row -%}
299313

300314
{%- block form_row -%}
315+
{%- set widget_attr = {} -%}
316+
{%- if help is not empty -%}
317+
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
318+
{%- endif -%}
301319
<div>
302320
{{- form_label(form) -}}
303321
{{- form_errors(form) -}}
304-
{{- form_widget(form) -}}
322+
{{- form_widget(form, widget_attr) -}}
323+
{{- form_help(form) -}}
305324
</div>
306325
{%- endblock form_row -%}
307326

src/Symfony/Bridge/Twig/Resources/views/Form/form_table_layout.html.twig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
{% use "form_div_layout.html.twig" %}
22

33
{%- block form_row -%}
4+
{%- set widget_attr = {} -%}
5+
{%- if help is not empty -%}
6+
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
7+
{%- endif -%}
48
<tr>
59
<td>
610
{{- form_label(form) -}}
711
</td>
812
<td>
913
{{- form_errors(form) -}}
10-
{{- form_widget(form) -}}
14+
{{- form_widget(form, widget_attr) -}}
15+
{{- form_help(form) -}}
1116
</td>
1217
</tr>
1318
{%- endblock form_row -%}

src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,16 @@
267267
{# Rows #}
268268

269269
{% block form_row -%}
270+
{%- set widget_attr = {} -%}
271+
{%- if help is not empty -%}
272+
{%- set widget_attr = {attr: {'aria-describedby': id ~"_help"}} -%}
273+
{%- endif -%}
270274
<div class="row">
271275
<div class="large-12 columns{% if (not compound or force_error|default(false)) and not valid %} error{% endif %}">
272-
{{ form_label(form) }}
273-
{{ form_widget(form) }}
274-
{{ form_errors(form) }}
276+
{{- form_label(form) -}}
277+
{{- form_widget(form, widget_attr) -}}
278+
{{- form_help(form) -}}
279+
{{- form_errors(form) -}}
275280
</div>
276281
</div>
277282
{%- endblock form_row %}

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3HorizontalLayoutTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra
6969
return (string) $this->renderer->searchAndRenderBlock($view, 'label', $vars);
7070
}
7171

72+
protected function renderHelp(FormView $view)
73+
{
74+
return (string) $this->renderer->searchAndRenderBlock($view, 'help');
75+
}
76+
7277
protected function renderErrors(FormView $view)
7378
{
7479
return (string) $this->renderer->searchAndRenderBlock($view, 'errors');

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra
8989
return (string) $this->renderer->searchAndRenderBlock($view, 'label', $vars);
9090
}
9191

92+
protected function renderHelp(FormView $view)
93+
{
94+
return (string) $this->renderer->searchAndRenderBlock($view, 'help');
95+
}
96+
9297
protected function renderErrors(FormView $view)
9398
{
9499
return (string) $this->renderer->searchAndRenderBlock($view, 'errors');

0 commit comments

Comments
 (0)
0