Skip to content

Commit 89686cc

Browse files
feature #31375 [Form] Add label_html attribute (przemyslaw-bogusz)
This PR was squashed before being merged into the 5.1-dev branch. Discussion ---------- [Form] Add label_html attribute | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT | Doc PR | symfony/symfony-docs#13316 I propose to add a new attribute to `BaseType` class so it is easy to include html tags in labels for, both, buttons and other elements that inherit from `FormType` class. This gives you an ability to add, e.g. a glyphicon to a button, or a link to a checkbox, simply inside the `FormBuilder`, which means you can just do ```twig {{ form(form) }} ``` inside a template. **Sidenotes** 1. I have started working on this two days ago and it the meantime @alexander-schranz made a similar proposition in #31358. If necessary, I can close my PR and @alexander-schranz can include my suggestions inside his PR. 1. I have just read in #29861 that @mpiot wanted to include this idea in his PR. With respect to @xabbuh's comments from that PR, I hope that my PR will be at least a good place to discuss, if the proposed feature is a good solution. Commits ------- 239fe04ff9 [Form] Add label_html attribute
2 parents 48df9ae + c012c55 commit 89686cc

File tree

5 files changed

+7
-0
lines changed

5 files changed

+7
-0
lines changed

Extension/Core/Type/BaseType.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
9797
'disabled' => $form->isDisabled(),
9898
'label' => $options['label'],
9999
'label_format' => $labelFormat,
100+
'label_html' => $options['label_html'],
100101
'multipart' => false,
101102
'attr' => $options['attr'],
102103
'block_prefixes' => $blockPrefixes,
@@ -127,6 +128,7 @@ public function configureOptions(OptionsResolver $resolver)
127128
'label' => null,
128129
'label_format' => null,
129130
'row_attr' => [],
131+
'label_html' => false,
130132
'label_translation_parameters' => [],
131133
'attr_translation_parameters' => [],
132134
'attr' => [],
@@ -137,5 +139,6 @@ public function configureOptions(OptionsResolver $resolver)
137139
$resolver->setAllowedTypes('block_prefix', ['null', 'string']);
138140
$resolver->setAllowedTypes('attr', 'array');
139141
$resolver->setAllowedTypes('row_attr', 'array');
142+
$resolver->setAllowedTypes('label_html', 'bool');
140143
}
141144
}

Tests/Fixtures/Descriptor/resolved_form_type_1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"label",
4747
"label_attr",
4848
"label_format",
49+
"label_html",
4950
"label_translation_parameters",
5051
"mapped",
5152
"method",

Tests/Fixtures/Descriptor/resolved_form_type_1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
2626
label
2727
label_attr
2828
label_format
29+
label_html
2930
label_translation_parameters
3031
mapped
3132
method

Tests/Fixtures/Descriptor/resolved_form_type_2.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"label",
2727
"label_attr",
2828
"label_format",
29+
"label_html",
2930
"label_translation_parameters",
3031
"mapped",
3132
"method",

Tests/Fixtures/Descriptor/resolved_form_type_2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")
2828
label
2929
label_attr
3030
label_format
31+
label_html
3132
label_translation_parameters
3233
mapped
3334
method

0 commit comments

Comments
 (0)