8000 [Form] Add label_html attribute · symfony/symfony@239fe04 · GitHub
[go: up one dir, main page]

Skip to content

Commit 239fe04

Browse files
przemyslaw-bogusznicolas-grekas
authored andcommitted
[Form] Add label_html attribute
1 parent 421c7f8 commit 239fe04

15 files changed

+288
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
{%- endif -%}
100100
{%- endif -%}
101101
<label{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
102-
{{- widget|raw }} {{ label is not same as(false) ? (translation_domain is same as(false) ? label : label|trans(label_translation_parameters, translation_domain)) -}}
102+
{{- widget|raw }} {{ label is not same as(false) ? (translation_domain is same as(false) ? (label_html is same as(false) ? label : label|raw) : (label_html is same as(false) ? label|trans(label_translation_parameters, translation_domain) : label|trans(label_translation_parameters, translation_domain)|raw)) -}}
103103
</label>
104104
{%- endif -%}
105105
{%- endblock checkbox_radio_label %}

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,21 @@
231231
{% set label = name|humanize %}
232232
{%- endif -%}
233233
{%- endif -%}
234-
<{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>{{ translation_domain is same as(false) ? label : label|trans(label_translation_parameters, translation_domain) }}{% block form_label_errors %}{{- form_errors(form) -}}{% endblock form_label_errors %}</{{ element|default('label') }}>
234+
<{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>
235+
{%- if translation_domain is same as(false) -%}
236+
{%- if label_html is same as(false) -%}
237+
{{- label -}}
238+
{%- else -%}
239+
{{- label|raw -}}
240+
{%- endif -%}
241+
{%- else -%}
242+
{%- if label_html is same as(false) -%}
243+
{{- label|trans(label_translation_parameters, translation_domain) -}}
244+
{%- else -%}
245+
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
246+
{%- endif -%}
247+
{%- endif -%}
248+
{% block form_label_errors %}{{- form_errors(form) -}}{% endblock form_label_errors %}</{{ element|default('label') }}>
235249
{%- else -%}
236250
{%- if errors|length > 0 -%}
237251
<div id="{{ id }}_errors" class="mb-2">
@@ -273,7 +287,21 @@
273287

274288
{{ widget|raw }}
275289
<label{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
276-
{{- label is not same as(false) ? (translation_domain is same as(false) ? label : label|trans(label_translation_parameters, translation_domain)) -}}
290+
{%- if label is not same as(false) -%}
291+
{%- if translation_domain is same as(false) -%}
292+
{%- if label_html is same as(false) -%}
293+
{{- label -}}
294+
{%- else -%}
295+
{{- label|raw -}}
296+
{%- endif -%}
297+
{%- else -%}
298+
{%- if label_html is same as(false) -%}
299+
{{- label|trans(label_translation_parameters, translation_domain) -}}
300+
{%- else -%}
301+
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
302+
{%- endif -%}
303+
{%- endif -%}
304+
{%- endif -%}
277305
{{- form_errors(form) -}}
278306
</label>
279307
{%- endif -%}

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,21 @@
232232
{% set label = name|humanize %}
233233
{%- endif -%}
234234
{%- endif -%}
235-
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{ translation_domain is same as(false) or label is same as(false) ? label : label|trans(label_translation_parameters, translation_domain) }}</button>
235+
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>
236+
{%- if translation_domain is same as(false) -%}
237+
{%- if label_html is same as(false) -%}
238+
{{- label -}}
239+
{%- else -%}
240+
{{- label|raw -}}
241+
{%- endif -%}
242+
{%- else -%}
243+
{%- if label_html is same as(false) -%}
244+
{{- label|trans(label_translation_parameters, translation_domain) -}}
245+
{%- else -%}
246+
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
247+
{%- endif -%}
248+
{%- endif -%}
249+
</button>
236250
{%- endblock button_widget -%}
237251

238252
{%- block submit_widget -%}
@@ -288,9 +302,17 @@
288302
{%- endif -%}
289303
<{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>
290304
{%- if translation_domain is same as(false) -%}
291-
{{- label -}}
305+
{%- if label_html is same as(false) -%}
306+
{{- label -}}
307+
{%- else -%}
308+
{{- label|raw -}}
309+
{%- endif -%}
292310
{%- else -%}
293-
{{- label|trans(label_translation_parameters, translation_domain) -}}
311+
{%- if label_html is same as(false) -%}
312+
{{- label|trans(label_translation_parameters, translation_domain) -}}
313+
{%- else -%}
314+
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
315+
{%- endif -%}
294316
{%- endif -%}
295317
</{{ element|default('label') }}>
296318
{%- endif -%}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,39 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
100100
);
101101
}
102102

103+
public function testLabelHtmlDefaultIsFalse()
104+
{
105+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
106+
'label' => '<b>Bolded label</b>',
107+
]);
108+
109+
$html = $this->renderLabel($form->createView(), null, [
110+
'label_attr' => [
111+
'class' => 'my&class',
112+
],
113+
]);
114+
115+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-sm-2 control-label required"][.="[trans]<b>Bolded label</b>[/trans]"]');
116+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-sm-2 control-label required"]/b[.="Bolded label"]', 0);
117+
}
118+
119+
public function testLabelHtmlIsTrue()
120+
{
121+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
122+
'label' => '<b>Bolded label</b>',
123+
'label_html' => true,
124+
]);
125+
126+
$html = $this->renderLabel($form->createView(), null, [
127+
'label_attr' => [
128+
'class' => 'my&class',
129+
],
130+
]);
131+
132+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-sm-2 control-label required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
133+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-sm-2 control-label required"]/b[.="Bolded label"]');
134+
}
135+
103136
public function testStartTag()
104137
{
105138
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, [

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,39 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
106106
);
107107
}
108108

109+
public function testLabelHtmlDefaultIsFalse()
110+
{
111+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
112+
'label' => '<b>Bolded label</b>',
113+
]);
114+
115+
$html = $this->renderLabel($form->createView(), null, [
116+
'label_attr' => [
117+
'class' => 'my&class',
118+
],
119+
]);
120+
121+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class control-label required"][.="[trans]<b>Bolded label</b>[/trans]"]');
122+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class control-label required"]/b[.="Bolded label"]', 0);
123+
}
124+
125+
public function testLabelHtmlIsTrue()
126+
{
127+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
128+
'label' => '<b>Bolded label</b>',
129+
'label_html' => true,
130+
]);
131+
132+
$html = $this->renderLabel($form->createView(), null, [
133+
'label_attr' => [
134+
'class' => 'my&class',
135+
],
136+
]);
137+
138+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class control-label required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
139+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class control-label required"]/b[.="Bolded label"]');
140+
}
141+
109142
public function testHelp()
110143
{
111144
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
@@ -2663,6 +2696,31 @@ public function testButtonlabelWithoutTranslation()
26632696
);
26642697
}
26652698

2699+
public function testButtonLabelHtmlDefaultIsFalse()
2700+
{
2701+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ButtonType', null, [
2702+
'label' => '<b>Click here!</b>',
2703+
]);
2704+
2705+
$html = $this->renderWidget($form->createView(), ['attr' => ['class' => 'my&class']]);
2706+
2707+
$this->assertMatchesXpath($html, '/button[@type="button"][@name="name"][.="[trans]<b>Click here!</b>[/trans]"][@class="my&class btn"]');
2708+
$this->assertMatchesXpath($html, '/button[@type="button"][@name="name"][@class="my&class btn"]/b[.="Click here!"]', 0);
2709+
}
2710+
2711+
public function testButtonLabelHtmlIsTrue()
2712+
{
2713+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ButtonType', null, [
2714+
'label' => '<b>Click here!</b>',
2715+
'label_html' => true,
2716+
]);
2717+
2718+
$html = $this->renderWidget($form->createView(), ['attr' => ['class' => 'my&class']]);
2719+
2720+
$this->assertMatchesXpath($html, '/button[@type="button"][@name="name"][.="[trans]<b>Click here!</b>[/trans]"][@class="my&class btn"]', 0);
2721+
$this->assertMatchesXpath($html, '/button[@type="button"][@name="name"][@class="my&class btn"]/b[.="Click here!"]');
2722+
}
2723+
26662724
public function testSubmit()
26672725
{
26682726
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\SubmitType');

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,39 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
132132
);
133133
}
134134

135+
public function testLabelHtmlDefaultIsFalse()
136+
{
137+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
138+
'label' => '<b>Bolded label</b>',
139+
]);
140+
141+
$html = $this->renderLabel($form->createView(), null, [
142+
'label_attr' => [
143+
'class' => 'my&class',
144+
],
145+
]);
146+
147+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-form-label col-sm-2 required"][.="[trans]<b>Bolded label</b>[/trans]"]');
148+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-form-label col-sm-2 required"]/b[.="Bolded label"]', 0);
149+
}
150+
151+
public function testLabelHtmlIsTrue()
152+
{
153+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
154+
'label' => '<b>Bolded label</b>',
155+
'label_html' => true,
156+
]);
157+
158+
$html = $this->renderLabel($form->createView(), null, [
159+
'label_attr' => [
160+
'class' => 'my&class',
161+
],
162+
]);
163+
164+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-form-label col-sm-2 required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
165+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-form-label col-sm-2 required"]/b[.="Bolded label"]');
166+
}
167+
135168
public function testLegendOnExpandedType()
136169
{
137170
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, [

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,39 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
141141
);
142142
}
143143

144+
public function testLabelHtmlDefaultIsFalse()
145+
{
146+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
147+
'label' => '<b>Bolded label</b>',
148+
]);
149+
150+
$html = $this->renderLabel($form->createView(), null, [
151+
'label_attr' => [
152+
'class' => 'my&class',
153+
],
154+
]);
155+
156+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]');
157+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]', 0);
158+
}
159+
160+
public function testLabelHtmlIsTrue()
161+
{
162+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
163+
'label' => '<b>Bolded label</b>',
164+
'label_html' => true,
165+
]);
166+
167+
$html = $this->renderLabel($form->createView(), null, [
168+
'label_attr' => [
169+
'class' => 'my&class',
170+
],
171+
]);
172+
173+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
174+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]');
175+
}
176+
144177
public function testLegendOnExpandedType()
145178
{
146179
$form = $this->factory->createNamed('name', ChoiceType::class, null, [

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,39 @@ public function testHelpHtmlIsTrue()
295295
);
296296
}
297297

298+
public function testLabelHtmlDefaultIsFalse()
299+
{
300+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
301+
'label' => '<b>Bolded label</b>',
302+
]);
303+
304+
$html = $this->renderLabel($form->createView(), null, [
305+
'label_attr' => [
306+
'class' => 'my&class',
307+
],
308+
]);
309+
310+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]');
311+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]', 0);
312+
}
313+
314+
public function testLabelHtmlIsTrue()
315+
{
316+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
317+
'label' => '<b>Bolded label</b>',
318+
'label_html' => true,
319+
]);
320+
321+
$html = $this->renderLabel($form->createView(), null, [
322+
'label_attr' => [
323+
'class' => 'my&class',
324+
],
325+
]);
326+
327+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
328+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]');
329+
}
330+
298331
protected function renderForm(FormView $view, array $vars = [])
299332
{
300333
return (string) $this->renderer->renderBlock($view, 'form', $vars);

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,39 @@ public function testHelpHtmlIsTrue()
181181
);
182182
}
183183

184+
public function testLabelHtmlDefaultIsFalse()
185+
{
186+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
187+
'label' => '<b>Bolded label</b>',
188+
]);
189+
190+
$html = $this->renderLabel($form->createView(), null, [
191+
'label_attr' => [
192+
'class' => 'my&class',
193+
],
194+
]);
195+
196+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]');
197+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]', 0);
198+
}
199+
200+
public function testLabelHtmlIsTrue()
201+
{
202+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
203+
'label' => '<b>Bolded label</b>',
204+
'label_html' => true,
205+
]);
206+
207+
$html = $this->renderLabel($form->createView(), null, [
208+
'label_attr' => [
209+
'class' => 'my&class',
210+
],
211+
]);
212+
213+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
214+
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]');
215+
}
216+
184217
protected function renderForm(FormView $view, array $vars = [])
185218
{
186219
return (string) $this->renderer->renderBlock($view, 'form', $vars);

src/Symfony/Bridge/Twig/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"symfony/asset": "^4.4|^5.0",
2626
"symfony/dependency-injection": "^4.4|^5.0",
2727
"symfony/finder": "^4.4|^5.0",
28-
"symfony/form": "^5.0",
28+
"symfony/form": "^5.1",
2929
"symfony/http-foundation": "^4.4|^5.0",
3030
"symfony/http-kernel": "^4.4|^5.0",
3131
"symfony/mime": "^4.4|^5.0",
@@ -48,7 +48,7 @@
4848
},
4949
"conflict": {
5050
"symfony/console": "<4.4",
51-
"symfony/form": "<5.0",
51+
"symfony/form": "<5.1",
5252
"symfony/http-foundation": "<4.4",
5353
"symfony/http-kernel": "<4.4",
5454
"symfony/translation": "<5.0",

0 commit comments

Comments
 (0)
0