From aad442da9d2738ca2956b138d6aa9bd2fc3dea74 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Fri, 26 Sep 2014 11:51:41 +0200 Subject: [PATCH] [Form] Added "label_format" option --- .../views/Form/form_div_layout.html.twig | 18 +++- .../views/Form/button_widget.html.php | 4 +- .../Resources/views/Form/form_label.html.php | 4 +- src/Symfony/Component/Form/CHANGELOG.md | 1 + .../Form/Extension/Core/Type/BaseType.php | 7 ++ .../Form/Tests/AbstractLayoutTest.php | 102 ++++++++++++++++++ 6 files changed, 132 insertions(+), 4 deletions(-) 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 699b2f8495..7df406f0ea 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 -%} {{ label|trans({}, translation_domain) }} {%- 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 64d4466688..9dac32fc99 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 4ed04cc392..27bba27c2b 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 4381726126..8ca730cd37 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 a6f8c430e1..c227edd6f2 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 @@ abstract class BaseType extends AbstractType $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 @@ abstract class BaseType extends AbstractType 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 @@ abstract class BaseType extends AbstractType 'full_name' => $fullName, 'disabled' => $form->isDisabled(), 'label' => $options['label'], + 'label_format' => $labelFormat, 'multipart' => false, 'attr' => $options['attr'], 'block_prefixes' => $blockPrefixes, @@ -111,6 +117,7 @@ abstract class BaseType extends AbstractType '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 4755e208e9..93409636fe 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -283,6 +283,108 @@ abstract class AbstractLayoutTest extends \Symfony\Component\Form\Test\FormInteg ); } + 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');