10000 feature #29861 [Form][TwigBridge] Add help_html (mpiot) · symfony/symfony@9876e3a · GitHub
[go: up one dir, main page]

Skip to content

Commit 9876e3a

Browse files
feature #29861 [Form][TwigBridge] Add help_html (mpiot)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Form][TwigBridge] Add help_html | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | no | License | MIT | Doc PR | symfony/symfony-docs#... Sometimes, when we use the form `help` option, we want to display it as HTML (add bold, italic, a span with a specific class, ...). For security reasons, we escape the `help` content. In this PR, I've added an `help_html` option, seted to false per default. When it set on true, the `help` content is no longer escaped. Commits ------- 33f5f85 [Form][TwigBridge] Add help_html option
2 parents edc4a0f + 33f5f85 commit 9876e3a

19 files changed

+464
-11
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* add `help_html` form option to display the `help` text as HTML
8+
49
4.2.0
510
-----
611

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,17 @@
180180
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' help-block')|trim}) -%}
181181
<span id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
182182
{%- if translation_domain is same as(false) -%}
183-
{{- help -}}
183+
{%- if help_html is same as(false) -%}
184+
{{- help -}}
185+
{%- else -%}
186+
{{- help|raw -}}
187+
{%- endif -%}
184188
{%- else -%}
185-
{{- help|trans({}, translation_domain) -}}
189+
{%- if help_html is same as(false) -%}
190+
{{- help|trans({}, translation_domain) -}}
191+
{%- else -%}
192+
{{- help|trans({}, translation_domain)|raw -}}
193+
{%- endif -%}
186194
{%- endif -%}
187195
</span>
188196
{%- endif -%}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,17 @@
305305
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' form-text text-muted')|trim}) -%}
306306
<small id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
307307
{%- if translation_domain is same as(false) -%}
308-
{{- help -}}
308+
{%- if help_html is same as(false) -%}
309+
{{- help -}}
310+
{%- else -%}
311+
{{- help|raw -}}
312+
{%- endif -%}
309313
{%- else -%}
310-
{{- help|trans({}, translation_domain) -}}
314+
{%- if help_html is same as(false) -%}
315+
{{- help|trans({}, translation_domain) -}}
316+
{%- else -%}
317+
{{- help|trans({}, translation_domain)|raw -}}
318+
{%- endif -%}
311319
{%- endif -%}
312320
</small>
313321
{%- endif -%}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,17 @@
294294
{%- set help_attr = help_attr|merge({class: (help_attr.class|default('') ~ ' help-text')|trim}) -%}
295295
<p id="{{ id }}_help"{% with { attr: help_attr } %}{{ block('attributes') }}{% endwith %}>
296296
{%- if translation_domain is same as(false) -%}
297-
{{- help -}}
297+
{%- if help_html is same as(false) -%}
298+
{{- help -}}
299+
{%- else -%}
300+
{{- help|raw -}}
301+
{%- endif -%}
298302
{%- else -%}
299-
{{- help|trans({}, translation_domain) -}}
303+
{%- if help_html is same as(false) -%}
304+
{{- help|trans({}, translation_domain) -}}
305+
{%- else -%}
306+
{{- help|trans({}, translation_domain)|raw -}}
307+
{%- endif -%}
300308
{%- endif -%}
301309
</p>
302310
{%- endif -%}

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,89 @@ public function testHelpAttr()
140140
);
141141
}
142142

143+
public function testHelpHtmlDefaultIsFalse()
144+
{
145+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
146+
'help' => 'Help <b>text</b> test!',
147+
]);
148+
149+
$view = $form->createView();
150+
$html = $this->renderHelp($view);
151+
152+
$this->assertMatchesXpath($html,
153+
'/span
154+
[@id="name_help"]
155+
[@class="help-block"]
156+
[.="[trans]Help <b>text</b> test![/trans]"]
157+
'
158+
);
159+
160+
$this->assertMatchesXpath($html,
161+
'/span
162+
[@id="name_help"]
163+
[@class="help-block"]
164+
/b
165+
[.="text"]
166+
', 0
167+
);
168+
}
169+
170+
public function testHelpHtmlIsFalse()
171+
{
172+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
173+
'help' => 'Help <b>text</b> test!',
174+
'help_html' => false,
175+
]);
176+
177+
$view = $form->createView();
178+
$html = $this->renderHelp($view);
179+
180+
$this->assertMatchesXpath($html,
181+
'/span
182+
[@id="name_help"]
183+
[@class="help-block"]
184+
[.="[trans]Help <b>text</b> test![/trans]"]
185+
'
186+
);
187+
188+
$this->assertMatchesXpath($html,
189+
'/span
190+
[@id="name_help"]
191+
[@class="help-block"]
192+
/b
193+
[.="text"]
194+
', 0
195+
);
196+
}
197+
198+
public function testHelpHtmlIsTrue()
199+
{
200+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
201+
'help' => 'Help <b>text</b> test!',
202+
'help_html' => true,
203+
]);
204+
205+
$view = $form->createView();
206+
$html = $this->renderHelp($view);
207+
208+
$this->assertMatchesXpath($html,
209+
'/span
210+
[@id="name_help"]
211+
[@class="help-block"]
212+
[.="[trans]Help <b>text</b> test![/trans]"]
213+
', 0
214+
);
215+
216+
$this->assertMatchesXpath($html,
217+
'/span
218+
[@id="name_help"]
219+
[@class="help-block"]
220+
/b
221+
[.="text"]
222+
'
223+
);
224+
}
225+
143226
public function testErrors()
144227
{
145228
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType');

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,89 @@ public function testHelpAttr()
197197
);
198198
}
199199

200+
public function testHelpHtmlDefaultIsFalse()
201+
{
202+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
203+
'help' => 'Help <b>text</b> test!',
204+
]);
205+
206+
$view = $form->createView();
207+
$html = $this->renderHelp($view);
208+
209+
$this->assertMatchesXpath($html,
210+
'/small
211+
[@id="name_help"]
212+
[@class="form-text text-muted"]
213+
[.="[trans]Help <b>text</b> test![/trans]"]
214+
'
215+
);
216+
217+
$this->assertMatchesXpath($html,
218+
'/small
219+
[@id="name_help"]
220+
[@class="form-text text-muted"]
221+
/b
222+
[.="text"]
223+
', 0
224+
);
225+
}
226+
227+
public function testHelpHtmlIsFalse()
228+
{
229+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
230+
'help' => 'Help <b>text</b> test!',
231+
'help_html' => false,
232+
]);
233+
234+
$view = $form->createView();
235+
$html = $this->renderHelp($view);
236+
237+
$this->assertMatchesXpath($html,
238+
'/small
239+
[@id="name_help"]
240+
[@class="form-text text-muted"]
241+
[.="[trans]Help <b>text</b> test![/trans]"]
242+
'
243+
);
244+
245+
$this->assertMatchesXpath($html,
246+
'/small
247+
[@id="name_help"]
248+
[@class="form-text text-muted"]
249+
/b
250+
[.="text"]
251+
', 0
252+
);
253+
}
254+
255+
public function testHelpHtmlIsTrue()
256+
{
257+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
258+
'help' => 'Help <b>text</b> test!',
259+
'help_html' => true,
260+
]);
261+
262+
$view = $form->createView();
263+
$html = $this->renderHelp($view);
264+
265+
$this->assertMatchesXpath($html,
266+
'/small
267+
[@id="name_help"]
268+
[@class="form-text text-muted"]
269+
[.="[trans]Help <b>text</b> test![/trans]"]
270+
', 0
271+
);
272+
273+
$this->assertMatchesXpath($html,
274+
'/small
275+
[@id="name_help"]
276+
[@class="form-text text-muted"]
277+
/b
278+
[.="text"]
279+
'
280+
);
281+
}
282+
200283
public function testErrors()
201284
{
202285
$form = $this->factory->createNamed('name', TextType::class);

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,89 @@ public function testHelpAttr()
210210
);
211211
}
212212

213+
public function testHelpHtmlDefaultIsFalse()
214+
{
215+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
216+
'help' => 'Help <b>text</b> test!',
217+
]);
218+
219+
$view = $form->createView();
220+
$html = $this->renderHelp($view);
221+
222+
$this->assertMatchesXpath($html,
223+
'/p
224+
[@id="name_help"]
225+
[@class="help-text"]
226+
[.="[trans]Help <b>text</b> test![/trans]"]
227+
'
228+
);
229+
230+
$this->assertMatchesXpath($html,
231+
'/p
232+
[@id="name_help"]
233+
[@class="help-text"]
234+
/b
235+
[.="text"]
236+
', 0
237+
);
238+
}
239+
240+
public function testHelpHtmlIsFalse()
241+
{
242+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
243+
'help' => 'Help <b>text</b> test!',
244+
'help_html' => false,
245+
]);
246+
247+
$view = $form->createView();
248+
$html = $this->renderHelp($view);
249+
250+
$this->assertMatchesXpath($html,
251+
'/p
252+
[@id="name_help"]
253+
[@class="help-text"]
254+
[.="[trans]Help <b>text</b> test![/trans]"]
255+
'
256+
);
257+
258+
$this->assertMatchesXpath($html,
259+
'/p
260+
[@id="name_help"]
261+
[@class="help-text"]
262+
/b
263+
[.="text"]
264+
', 0
265+
);
266+
}
267+
268+
public function testHelpHtmlIsTrue()
269+
{
270+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
271+
'help' => 'Help <b>text</b> test!',
272+
'help_html' => true,
273+
]);
274+
275+
$view = $form->createView();
276+
$html = $this->renderHelp($view);
277+
278+
$this->assertMatchesXpath($html,
279+
'/p
280+
[@id="name_help"]
281+
[@class="help-text"]
282+
[.="[trans]Help <b>text</b> test![/trans]"]
283+
', 0
284+
);
285+
286+
$this->assertMatchesXpath($html,
287+
'/p
288+
[@id="name_help"]
289+
[@class="help-text"]
290+
/b
291+
[.="text"]
292+
'
293+
);
294+
}
295+
213296
protected function renderForm(FormView $view, array $vars = [])
214297
{
215298
return (string) $this->renderer->renderBlock($view, 'form', $vars);

0 commit comments

Comments
 (0)
0