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{% < 4161 span class="pl-k">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');

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

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

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

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

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

95+
protected function renderHelp(FormView $view)
96+
{
97+
return (string) $this->renderer->searchAndRenderBlock($view, 'help');
98+
}
99+
95100
protected function renderErrors(FormView $view)
96101
{
97102
return (string) $this->renderer->searchAndRenderBlock($view, 'errors');

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

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

182+
protected function renderHelp(FormView $view)
183+
{
184+
return (string) $this->renderer->searchAndRenderBlock($view, 'help');
185+
}
186+
182187
protected function renderErrors(FormView $view)
183188
{
184189
return (string) $this->renderer->searchAndRenderBlock($view, 'errors');

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

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

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

src/Symfony/Bridge/Twig/UndefinedCallableHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class UndefinedCallableHandler
3434
'form_widget' => 'form',
3535
'form_errors' => 'form',
3636
'form_label' => 'form',
37+
'form_help' => 'form',
3738
'form_row' => 'form',
3839
'form_rest' => 'form',
3940
'form' => 'form',

src/Symfony/Bridge/Twig/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"symfony/asset": "~3.4|~4.0",
2424
"symfony/dependency-injection": "~3.4|~4.0",
2525
"symfony/finder": "~3.4|~4.0",
26-
"symfony/form": "^3.4.7|^4.0.7",
26+
"symfony/form": "^4.1",
2727
"symfony/http-foundation": "~3.4|~4.0",
2828
"symfony/http-kernel": "~3.4|~4.0",
2929
"symfony/polyfill-intl-icu": "~1.0",
@@ -41,7 +41,7 @@
4141
"symfony/workflow": "~3.4|~4.0"
4242
},
4343
"conflict": {
44-
"symfony/form": "<3.4.7|<4.0.7,>=4.0",
44+
"symfony/form": "<4.1",
4545
"symfony/console": "<3.4"
4646
},
4747
"suggest": {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php if (!empty($help)): ?>
2+
<p id="<?php echo $view->escape($id); ?>_help" class="help-text"><?php echo $view->escape(false !== $translation_domain ? $view['translator']->trans($help, array(), $translation_domain) : $help); ?></p>
3+
<?php endif; ?>
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<div>
2-
<?php echo $view['form']->label($form) ?>
3-
<?php echo $view['form']->errors($form) ?>
4-
<?php echo $view['form']->widget($form) ?>
2+
<?php $widgetAttr = empty($help) ? array() : array('attr' => array('aria-describedby' => $id.'_help')); ?>
3+
<?php echo $view['form']->label($form); ?>
4+
<?php echo $view['form']->errors($form); ?>
5+
<?php echo $view['form']->widget($form, $widgetAttr); ?>
6+
<?php echo $view['form']->help($form); ?>
57
</div>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>"<?php if ($disabled): ?> disabled="disabled"<?php endif ?>
22
<?php if ($required): ?> required="required"<?php endif ?>
3-
<?php echo $attr ? ' '.$view['form']->block($form, 'attributes') : '' ?>
3+
<?php echo $attr ? ' '.$view['form']->block($form, 'attributes') : '' ?>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<tr>
2+
<?php $widgetAttr = empty($help) ? array() : array('attr' => array('aria-describedby' => $id.'_help')); ?>
23
<td>
34
<?php echo $view['form']->label($form); ?>
45
</td>
56
<td>
67
<?php echo $view['form']->errors($form); ?>
7-
<?php echo $view['form']->widget($form); ?>
8+
<?php echo $view['form']->widget($form, $widgetAttr); ?>
9+
<?php echo $view['form']->help($form); ?>
810
</td>
911
</tr>

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ public function label(FormView $view, $label = null, array $variables = array())
169169
return $this->renderer->searchAndRenderBlock($view, 'label', $variables);
170170
}
171171

172+
/**
173+
* Renders the help of the given view.
174+
*
175+
* @param FormView $view The parent view
176+
*
177+
* @return string The HTML markup
178+
*/
179+
public function help(FormView $view): string
180+
{
181+
return $this->renderer->searchAndRenderBlock($view, 'help');
182+
}
183+
172184
/**
173185
* Renders the errors of the given view.
174186
*

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra
9191
return (string) $this->engine->get('form')->label($view, $label, $vars);
9292
}
9393

94+
protected function renderHelp(FormView $view)
95+
{
96+
return (string) $this->engine->get('form')->help($view);
97+
}
98+
9499
protected function renderErrors(FormView $view)
95100
{
96101
return (string) $this->engine->get('form')->errors($view);

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableLayoutTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ protected function renderLabel(FormView $view, $label = null, array $vars = arra
9292
return (string) $this->engine->get('form')->label($view, $label, $vars);
9393
}
9494

95+
protected function renderHelp(FormView $view)
96+
{
97+
return (string) $this->engine->get('form')->help($view);
98+
}
99+
95100
protected function renderErrors(FormView $view)
96101
{
97102
return (string) $this->engine->get('form')->errors($view);

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"symfony/dom-crawler": "~3.4|~4.0",
4040
"symfony/polyfill-intl-icu": "~1.0",
4141
"symfony/security": "~3.4|~4.0",
42-
"symfony/form": "~3.4|~4.0",
42+
"symfony/form": "^4.1",
4343
"symfony/expression-language": "~3.4|~4.0",
4444
"symfony/process": "~3.4|~4.0",
4545
"symfony/security-core": "~3.4|~4.0",
@@ -65,7 +65,7 @@
6565
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
6666
"symfony/asset": "<3.4",
6767
"symfony/console": "<3.4",
68-
"symfony/form": "<3.4",
68+
"symfony/form": "<4.1",
6969
"symfony/property-info": "<3.4",
7070
"symfony/serializer": "<4.1",
7171
"symfony/stopwatch": "<3.4",

0 commit comments

Comments
 (0)
0