Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 832c78f

Browse filesBrowse files
committed
feature #12050 [Form] Added "label_format" option (webmozart)
This PR was merged into the 2.6-dev branch. Discussion ---------- [Form] Added "label_format" option | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11456 | License | MIT | Doc PR | TODO This PR replaces #11456. It adds a "label_format" option which allows to configure a format for generating labels. Two placeholders are available: `%name%` and `%id%`. **Feedback wanted**: Should we change the placeholders to `{{ name }}` and `{{ id }}`? The option is inherited from the parent form if not set explicitly on a field: ```php $form = $this->createForm('myform', $data, array('label_format' => 'form.label.%id%')); ``` Follow-up PR: Make the default label format and translation domain configurable in config.yml. Commits ------- aad442d [Form] Added "label_format" option
2 parents 95d68a1 + aad442d commit 832c78f
Copy full SHA for 832c78f

File tree

Expand file treeCollapse file tree

6 files changed

+132
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

6 files changed

+132
-4
lines changed
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
+16-2Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,14 @@
177177

178178
{% block button_widget -%}
179179
{% if label is empty -%}
180-
{% set label = name|humanize %}
180+
{%- if label_format is not empty -%}
181+
{% set label = label_format|replace({
182+
'%name%': name,
183+
'%id%': id,
184+
}) %}
185+
{%- else -%}
186+
{% set label = name|humanize %}
187+
{%- endif -%}
181188
{%- endif -%}
182189
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{ label|trans({}, translation_domain) }}</button>
183190
{%- endblock button_widget %}
@@ -203,7 +210,14 @@
203210
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
204211
{%- endif %}
205212
{% if label is empty -%}
206-
{% set label = name|humanize %}
213+
{%- if label_format is not empty -%}
214+
{% set label = label_format|replace({
215+
'%name%': name,
216+
'%id%': id,
217+
}) %}
218+
{%- else -%}
219+
{% set label = name|humanize %}
220+
{%- endif -%}
207221
{%- endif -%}
208222
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ label|trans({}, translation_domain) }}</label>
209223
{%- endif %}
Collapse file
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
1+
<?php if (!$label) { $label = isset($label_format)
2+
? strtr($label_format, array('%name%' => $name, '%id%' => $id))
3+
: $view['form']->humanize($name); } ?>
24
<button type="<?php echo isset($type) ? $view->escape($type) : 'button' ?>" <?php echo $view['form']->block($form, 'button_attributes') ?>><?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></button>
Collapse file
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php if (false !== $label): ?>
22
<?php if ($required) { $label_attr['class'] = trim((isset($label_attr['class']) ? $label_attr['class'] : '').' required'); } ?>
33
<?php if (!$compound) { $label_attr['for'] = $id; } ?>
4-
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
4+
<?php if (!$label) { $label = isset($label_format)
5+
? strtr($label_format, array('%name%' => $name, '%id%' => $id))
6+
: $view['form']->humanize($name); } ?>
57
<label <?php foreach ($label_attr as $k => $v) { printf('%s="%s" ', $view->escape($k), $view->escape($v)); } ?>><?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></label>
68
<?php endif ?>
Collapse file

‎src/Symfony/Component/Form/CHANGELOG.md‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added "html5" option to Date, Time and DateTimeFormType to be able to
88
enable/disable HTML5 input date when widget option is "single_text"
9+
* added "label_format" option with possible placeholders "%name%" and "%id%"
910

1011
2.5.0
1112
------
Collapse file

‎src/Symfony/Component/Form/Extension/Core/Type/BaseType.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/BaseType.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
4444
$name = $form->getName();
4545
$blockName = $options['block_name'] ?: $form->getName();
4646
$translationDomain = $options['translation_domain'];
47+
$labelFormat = $options['label_format'];
4748

4849
if ($view->parent) {
4950
if ('' !== ($parentFullName = $view->parent->vars['full_name'])) {
@@ -59,6 +60,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)
5960
if (!$translationDomain) {
6061
$translationDomain = $view->parent->vars['translation_domain'];
6162
}
63+
64+
if (!$labelFormat) {
65+
$labelFormat = $view->parent->vars['label_format'];
66+
}
6267
} else {
6368
$id = $name;
6469
$fullName = $name;
@@ -87,6 +92,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
8792
'full_name' => $fullName,
8893
'disabled' => $form->isDisabled(),
8994
'label' => $options['label'],
95+
'label_format' => $labelFormat,
9096
'multipart' => false,
9197
'attr' => $options['attr'],
9298
'block_prefixes' => $blockPrefixes,
@@ -111,6 +117,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
111117
'block_name' => null,
112118
'disabled' => false,
113119
'label' => null,
120+
'label_format' => null,
114121
'attr' => array(),
115122
'translation_domain' => null,
116123
'auto_initialize' => true,
Collapse file

‎src/Symfony/Component/Form/Tests/AbstractLayoutTest.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
+102Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,108 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
283283
);
284284
}
285285

286+
public function testLabelFormatName()
287+
{
288+
$form = $this->factory->createNamedBuilder('myform')
289+
->add('myfield', 'text')
290+
->getForm();
291+
$view = $form->get('myfield')->createView();
292+
$html = $this->renderLabel($view, null, array('label_format' => 'form.%name%'));
293+
294+
$this->assertMatchesXpath($html,
295+
'/label
296+
[@for="myform_myfield"]
297+
[.="[trans]form.myfield[/trans]"]
298+
'
299+
);
300+
}
301+
302+
public function testLabelFormatId()
303+
{
304+
$form = $this->factory->createNamedBuilder('myform')
305+
->add('myfield', 'text')
306+
->getForm();
307+
$view = $form->get('myfield')->createView();
308+
$html = $this->renderLabel($view, null, array('label_format' => 'form.%id%'));
309+
310+
$this->assertMatchesXpath($html,
311+
'/label
312+
[@for="myform_myfield"]
313+
[.="[trans]form.myform_myfield[/trans]"]
314+
'
315+
);
316+
}
317+
318+
public function testLabelFormatAsFormOption()
319+
{
320+
$options = array('label_format' => 'form.%name%');
321+
322+
$form = $this->factory->createNamedBuilder('myform', 'form', null, $options)
323+
->add('myfield', 'text')
324+
->getForm();
325+
$view = $form->get('myfield')->createView();
326+
$html = $this->renderLabel($view);
327+
328+
$this->assertMatchesXpath($html,
329+
'/label
330+
[@for="myform_myfield"]
331+
[.="[trans]form.myfield[/trans]"]
332+
'
333+
);
334+
}
335+
336+
public function testLabelFormatOverriddenOption()
337+
{
338+
$options = array('label_format' => 'form.%name%');
339+
340+
$form = $this->factory->createNamedBuilder('myform', 'form', null, $options)
341+
->add('myfield', 'text', array('label_format' => 'field.%name%'))
342+
->getForm();
343+
$view = $form->get('myfield')->createView();
344+
$html = $this->renderLabel($view);
345+
346+
$this->assertMatchesXpath($html,
347+
'/label
348+
[@for="myform_myfield"]
349+
[.="[trans]field.myfield[/trans]"]
350+
'
351+
);
352+
}
353+
354+
public function testLabelFormatOnButton()
355+
{
356+
$form = $this->factory->createNamedBuilder('myform')
357+
->add('mybutton', 'button')
358+
->getForm();
359+
$view = $form->get('mybutton')->createView();
360+
$html = $this->renderWidget($view, array('label_format' => 'form.%name%'));
361+
362+
$this->assertMatchesXpath($html,
363+
'/button
364+
[@type="button"]
365+
[@name="myform[mybutton]"]
366+
[.="[trans]form.mybutton[/trans]"]
367+
'
368+
);
369+
}
370+
371+
public function testLabelFormatOnButtonId()
372+
{
373+
$form = $this->factory->createNamedBuilder('myform')
374+
->add('mybutton', 'button')
375+
->getForm();
376+
$view = $form->get('mybutton')->createView();
377+
$html = $this->renderWidget($view, array('label_format' => 'form.%id%'));
378+
379+
$this->assertMatchesXpath($html,
380+
'/button
381+
[@type="button"]
382+
[@name="myform[mybutton]"]
383+
[.="[trans]form.myform_mybutton[/trans]"]
384+
'
385+
);
386+
}
387+
286388
public function testErrors()
287389
{
288390
$form = $this->factory->createNamed('name', 'text');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.