Skip to content

[Form] Add label_html attribute #31375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
{%- endif -%}
{%- endif -%}
<label{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
{{- widget|raw }} {{ label is not same as(false) ? (translation_domain is same as(false) ? label : label|trans(label_translation_parameters, translation_domain)) -}}
{{- 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)) -}}
</label>
{%- endif -%}
{%- endblock checkbox_radio_label %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,21 @@
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<{{ 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') }}>
<{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>
{%- if translation_domain is same as(false) -%}
{%- if label_html is same as(false) -%}
{{- label -}}
{%- else -%}
{{- label|raw -}}
{%- endif -%}
{%- else -%}
{%- if label_html is same as(false) -%}
{{- label|trans(label_translation_parameters, translation_domain) -}}
{%- else -%}
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
{%- endif -%}
{%- endif -%}
{% block form_label_errors %}{{- form_errors(form) -}}{% endblock form_label_errors %}</{{ element|default('label') }}>
{%- else -%}
{%- if errors|length > 0 -%}
<div id="{{ id }}_errors" class="mb-2">
Expand Down Expand Up @@ -273,7 +287,21 @@

{{ widget|raw }}
<label{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
{{- label is not same as(false) ? (translation_domain is same as(false) ? label : label|trans(label_translation_parameters, translation_domain)) -}}
{%- if label is not same as(false) -%}
{%- if translation_domain is same as(false) -%}
{%- if label_html is same as(false) -%}
{{- label -}}
{%- else -%}
{{- label|raw -}}
{%- endif -%}
{%- else -%}
{%- if label_html is same as(false) -%}
{{- label|trans(label_translation_parameters, translation_domain) -}}
{%- else -%}
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{{- form_errors(form) -}}
</label>
{%- endif -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,21 @@
{% set label = name|humanize %}
{%- endif -%}
{%- endif -%}
<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>
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>
{%- if translation_domain is same as(false) -%}
{%- if label_html is same as(false) -%}
{{- label -}}
{%- else -%}
{{- label|raw -}}
{%- endif -%}
{%- else -%}
{%- if label_html is same as(false) -%}
{{- label|trans(label_translation_parameters, translation_domain) -}}
{%- else -%}
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
{%- endif -%}
{%- endif -%}
</button>
{%- endblock button_widget -%}

{%- block submit_widget -%}
Expand Down Expand Up @@ -288,9 +302,17 @@
{%- endif -%}
<{{ element|default('label') }}{% if label_attr %}{% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}{% endif %}>
{%- if translation_domain is same as(false) -%}
{{- label -}}
{%- if label_html is same as(false) -%}
{{- label -}}
{%- else -%}
{{- label|raw -}}
{%- endif -%}
{%- else -%}
{{- label|trans(label_translation_parameters, translation_domain) -}}
{%- if label_html is same as(false) -%}
{{- label|trans(label_translation_parameters, translation_domain) -}}
{%- else -%}
{{- label|trans(label_translation_parameters, translation_domain)|raw -}}
{%- endif -%}
{%- endif -%}
</{{ element|default('label') }}>
{%- endif -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,39 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
);
}

public function testLabelHtmlDefaultIsFalse()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-sm-2 control-label required"][.="[trans]<b>Bolded label</b>[/trans]"]');
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-sm-2 control-label required"]/b[.="Bolded label"]', 0);
}

public function testLabelHtmlIsTrue()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
'label_html' => true,
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-sm-2 control-label required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-sm-2 control-label required"]/b[.="Bolded label"]');
}

public function testStartTag()
{
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,39 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
);
}

public function testLabelHtmlDefaultIsFalse()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class control-label required"][.="[trans]<b>Bolded label</b>[/trans]"]');
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class control-label required"]/b[.="Bolded label"]', 0);
}

public function testLabelHtmlIsTrue()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
'label_html' => true,
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class control-label required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class control-label required"]/b[.="Bolded label"]');
}

public function testHelp()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
Expand Down Expand Up @@ -2663,6 +2696,31 @@ public function testButtonlabelWithoutTranslation()
);
}

public function testButtonLabelHtmlDefaultIsFalse()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ButtonType', null, [
'label' => '<b>Click here!</b>',
]);

$html = $this->renderWidget($form->createView(), ['attr' => ['class' => 'my&class']]);

$this->assertMatchesXpath($html, '/button[@type="button"][@name="name"][.="[trans]<b>Click here!</b>[/trans]"][@class="my&class btn"]');
$this->assertMatchesXpath($html, '/button[@type="button"][@name="name"][@class="my&class btn"]/b[.="Click here!"]', 0);
}

public function testButtonLabelHtmlIsTrue()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ButtonType', null, [
'label' => '<b>Click here!</b>',
'label_html' => true,
]);

$html = $this->renderWidget($form->createView(), ['attr' => ['class' => 'my&class']]);

$this->assertMatchesXpath($html, '/button[@type="button"][@name="name"][.="[trans]<b>Click here!</b>[/trans]"][@class="my&class btn"]', 0);
$this->assertMatchesXpath($html, '/button[@type="button"][@name="name"][@class="my&class btn"]/b[.="Click here!"]');
}

public function testSubmit()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\SubmitType');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,39 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
);
}

public function testLabelHtmlDefaultIsFalse()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-form-label col-sm-2 required"][.="[trans]<b>Bolded label</b>[/trans]"]');
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-form-label col-sm-2 required"]/b[.="Bolded label"]', 0);
}

public function testLabelHtmlIsTrue()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
'label_html' => true,
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-form-label col-sm-2 required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class col-form-label col-sm-2 required"]/b[.="Bolded label"]');
}

public function testLegendOnExpandedType()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,39 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
);
}

public function testLabelHtmlDefaultIsFalse()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]');
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]', 0);
}

public function testLabelHtmlIsTrue()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
'label_html' => true,
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]');
}

public function testLegendOnExpandedType()
{
$form = $this->factory->createNamed('name', ChoiceType::class, null, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,39 @@ public function testHelpHtmlIsTrue()
);
}

public function testLabelHtmlDefaultIsFalse()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]');
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]', 0);
}

public function testLabelHtmlIsTrue()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
'label_html' => true,
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]');
}

protected function renderForm(FormView $view, array $vars = [])
{
return (string) $this->renderer->renderBlock($view, 'form', $vars);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,39 @@ public function testHelpHtmlIsTrue()
);
}

public function testLabelHtmlDefaultIsFalse()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]');
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]', 0);
}

public function testLabelHtmlIsTrue()
{
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\TextType', null, [
'label' => '<b>Bolded label</b>',
'label_html' => true,
]);

$html = $this->renderLabel($form->createView(), null, [
'label_attr' => [
'class' => 'my&class',
],
]);

$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"][.="[trans]<b>Bolded label</b>[/trans]"]', 0);
$this->assertMatchesXpath($html, '/label[@for="name"][@class="my&class required"]/b[.="Bolded label"]');
}

protected function renderForm(FormView $view, array $vars = [])
{
return (string) $this->renderer->renderBlock($view, 'form', $vars);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"symfony/asset": "^4.4|^5.0",
"symfony/dependency-injection": "^4.4|^5.0",
"symfony/finder": "^4.4|^5.0",
"symfony/form": "^5.0",
"symfony/form": "^5.1",
"symfony/http-foundation": "^4.4|^5.0",
"symfony/http-kernel": "^4.4|^5.0",
"symfony/mime": "^4.4|^5.0",
Expand All @@ -48,7 +48,7 @@
},
"conflict": {
"symfony/console": "<4.4",
"symfony/form": "<5.0",
"symfony/form": "<5.1",
"symfony/http-foundation": "<4.4",
"symfony/http-kernel": "<4.4",
"symfony/translation": "<5.0",
Expand Down
Loading