diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
index 699b2f849535e..7df406f0ea898 100644
--- a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
+++ b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
@@ -177,7 +177,14 @@
{% block button_widget -%}
{% if label is empty -%}
- {% set label = name|humanize %}
+ {%- if label_format is not empty -%}
+ {% set label = label_format|replace({
+ '%name%': name,
+ '%id%': id,
+ }) %}
+ {%- else -%}
+ {% set label = name|humanize %}
+ {%- endif -%}
{%- endif -%}
{%- endblock button_widget %}
@@ -203,7 +210,14 @@
{% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %}
{%- endif %}
{% if label is empty -%}
- {% set label = name|humanize %}
+ {%- if label_format is not empty -%}
+ {% set label = label_format|replace({
+ '%name%': name,
+ '%id%': id,
+ }) %}
+ {%- else -%}
+ {% set label = name|humanize %}
+ {%- endif -%}
{%- endif -%}
{%- endif %}
diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/button_widget.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/button_widget.html.php
index 64d44666888ba..9dac32fc994c0 100644
--- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/button_widget.html.php
+++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/button_widget.html.php
@@ -1,2 +1,4 @@
-humanize($name); } ?>
+ $name, '%id%' => $id))
+ : $view['form']->humanize($name); } ?>
diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_label.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_label.html.php
index 4ed04cc392f1e..27bba27c2b5cb 100644
--- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_label.html.php
+++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_label.html.php
@@ -1,6 +1,8 @@
-humanize($name); } ?>
+ $name, '%id%' => $id))
+ : $view['form']->humanize($name); } ?>
diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md
index 4381726126d48..8ca730cd37e04 100644
--- a/src/Symfony/Component/Form/CHANGELOG.md
+++ b/src/Symfony/Component/Form/CHANGELOG.md
@@ -6,6 +6,7 @@ CHANGELOG
* added "html5" option to Date, Time and DateTimeFormType to be able to
enable/disable HTML5 input date when widget option is "single_text"
+ * added "label_format" option with possible placeholders "%name%" and "%id%"
2.5.0
------
diff --git a/src/Symfony/Component/Form/Extension/Core/Type/BaseType.php b/src/Symfony/Component/Form/Extension/Core/Type/BaseType.php
index a6f8c430e1373..c227edd6f289a 100644
--- a/src/Symfony/Component/Form/Extension/Core/Type/BaseType.php
+++ b/src/Symfony/Component/Form/Extension/Core/Type/BaseType.php
@@ -44,6 +44,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$name = $form->getName();
$blockName = $options['block_name'] ?: $form->getName();
$translationDomain = $options['translation_domain'];
+ $labelFormat = $options['label_format'];
if ($view->parent) {
if ('' !== ($parentFullName = $view->parent->vars['full_name'])) {
@@ -59,6 +60,10 @@ public function buildView(FormView $view, FormInterface $form, array $options)
if (!$translationDomain) {
$translationDomain = $view->parent->vars['translation_domain'];
}
+
+ if (!$labelFormat) {
+ $labelFormat = $view->parent->vars['label_format'];
+ }
} else {
$id = $name;
$fullName = $name;
@@ -87,6 +92,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
'full_name' => $fullName,
'disabled' => $form->isDisabled(),
'label' => $options['label'],
+ 'label_format' => $labelFormat,
'multipart' => false,
'attr' => $options['attr'],
'block_prefixes' => $blockPrefixes,
@@ -111,6 +117,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'block_name' => null,
'disabled' => false,
'label' => null,
+ 'label_format' => null,
'attr' => array(),
'translation_domain' => null,
'auto_initialize' => true,
diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
index 4755e208e9a51..93409636fe633 100644
--- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
+++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
@@ -283,6 +283,108 @@ public function testLabelWithCustomTextAsOptionAndCustomAttributesPassedDirectly
);
}
+ public function testLabelFormatName()
+ {
+ $form = $this->factory->createNamedBuilder('myform')
+ ->add('myfield', 'text')
+ ->getForm();
+ $view = $form->get('myfield')->createView();
+ $html = $this->renderLabel($view, null, array('label_format' => 'form.%name%'));
+
+ $this->assertMatchesXpath($html,
+'/label
+ [@for="myform_myfield"]
+ [.="[trans]form.myfield[/trans]"]
+'
+ );
+ }
+
+ public function testLabelFormatId()
+ {
+ $form = $this->factory->createNamedBuilder('myform')
+ ->add('myfield', 'text')
+ ->getForm();
+ $view = $form->get('myfield')->createView();
+ $html = $this->renderLabel($view, null, array('label_format' => 'form.%id%'));
+
+ $this->assertMatchesXpath($html,
+'/label
+ [@for="myform_myfield"]
+ [.="[trans]form.myform_myfield[/trans]"]
+'
+ );
+ }
+
+ public function testLabelFormatAsFormOption()
+ {
+ $options = array('label_format' => 'form.%name%');
+
+ $form = $this->factory->createNamedBuilder('myform', 'form', null, $options)
+ ->add('myfield', 'text')
+ ->getForm();
+ $view = $form->get('myfield')->createView();
+ $html = $this->renderLabel($view);
+
+ $this->assertMatchesXpath($html,
+'/label
+ [@for="myform_myfield"]
+ [.="[trans]form.myfield[/trans]"]
+'
+ );
+ }
+
+ public function testLabelFormatOverriddenOption()
+ {
+ $options = array('label_format' => 'form.%name%');
+
+ $form = $this->factory->createNamedBuilder('myform', 'form', null, $options)
+ ->add('myfield', 'text', array('label_format' => 'field.%name%'))
+ ->getForm();
+ $view = $form->get('myfield')->createView();
+ $html = $this->renderLabel($view);
+
+ $this->assertMatchesXpath($html,
+'/label
+ [@for="myform_myfield"]
+ [.="[trans]field.myfield[/trans]"]
+'
+ );
+ }
+
+ public function testLabelFormatOnButton()
+ {
+ $form = $this->factory->createNamedBuilder('myform')
+ ->add('mybutton', 'button')
+ ->getForm();
+ $view = $form->get('mybutton')->createView();
+ $html = $this->renderWidget($view, array('label_format' => 'form.%name%'));
+
+ $this->assertMatchesXpath($html,
+'/button
+ [@type="button"]
+ [@name="myform[mybutton]"]
+ [.="[trans]form.mybutton[/trans]"]
+'
+ );
+ }
+
+ public function testLabelFormatOnButtonId()
+ {
+ $form = $this->factory->createNamedBuilder('myform')
+ ->add('mybutton', 'button')
+ ->getForm();
+ $view = $form->get('mybutton')->createView();
+ $html = $this->renderWidget($view, array('label_format' => 'form.%id%'));
+
+ $this->assertMatchesXpath($html,
+'/button
+ [@type="button"]
+ [@name="myform[mybutton]"]
+ [.="[trans]form.myform_mybutton[/trans]"]
+'
+ );
+ }
+
public function testErrors()
{
$form = $this->factory->createNamed('name', 'text');