From c1e4166eadd4c01eac09755a0f985ddf3c902011 Mon Sep 17 00:00:00 2001 From: Kris Wallsmith Date: Sun, 24 Jun 2012 07:20:53 -0700 Subject: [PATCH] moved create of default form label to view layer --- UPGRADE-2.1.md | 16 ++++++++++++++++ .../Bridge/Twig/Extension/FormExtension.php | 12 ++++++++++++ .../views/Form/form_div_layout.html.twig | 3 +++ .../Resources/views/Form/form_label.html.php | 1 + .../Templating/Helper/FormHelper.php | 5 +++++ .../Form/Extension/Core/Type/FormType.php | 7 +------ .../Tests/Extension/Core/Type/FormTypeTest.php | 8 -------- 7 files changed, 38 insertions(+), 14 deletions(-) diff --git a/UPGRADE-2.1.md b/UPGRADE-2.1.md index 3389cf0563..eb6404402f 100644 --- a/UPGRADE-2.1.md +++ b/UPGRADE-2.1.md @@ -460,6 +460,22 @@ {% endfor %} ``` + * Creation of default labels has been moved to the view layer. You will need + to incorporate this logic into any custom `form_label` templates to + accommodate those cases when the `label` option has not been explicitly + set. + + ``` + {% block form_label %} + {% if label is empty %} + {% set label = name|humanize %} + {% endif %} + + {# ... #} + + {% endblock %} + ```` + #### Other BC Breaks * The order of the first two arguments of the methods `createNamed` and diff --git a/src/Symfony/Bridge/Twig/Extension/FormExtension.php b/src/Symfony/Bridge/Twig/Extension/FormExtension.php index cb6a866038..d21fe056ca 100644 --- a/src/Symfony/Bridge/Twig/Extension/FormExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/FormExtension.php @@ -91,6 +91,13 @@ class FormExtension extends \Twig_Extension ); } + public function getFilters() + { + return array( + 'humanize' => new \Twig_Filter_Function(__NAMESPACE__.'\humanize'), + ); + } + public function isChoiceGroup($label) { return FormUtil::isChoiceGroup($label); @@ -364,3 +371,8 @@ class FormExtension extends \Twig_Extension return $blocks; } } + +function humanize($text) +{ + return ucfirst(trim(strtolower(preg_replace('/[_\s]+/', ' ', $text)))); +} 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 bff18c6e1a..1d15e502b1 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 @@ -225,6 +225,9 @@ {% if required %} {% set label_attr = label_attr|merge({'class': (label_attr.class|default('') ~ ' required')|trim}) %} {% endif %} + {% if label is empty %} + {% set label = name|humanize %} + {% endif %} {{ label|trans({}, translation_domain) }} {% endspaceless %} {% endblock form_label %} 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 c8f7947173..48dd147a60 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,3 +1,4 @@ +humanize($name); } ?> diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php index 517186b3bd..fd8c3deeab 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php @@ -314,6 +314,11 @@ class FormHelper extends Helper return trim($this->engine->render($template, $variables)); } + public function humanize($text) + { + return ucfirst(trim(strtolower(preg_replace('/[_\s]+/', ' ', $text)))); + } + public function getName() { return 'form'; diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php index 32503f620d..14d2203e98 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php @@ -112,7 +112,7 @@ class FormType extends AbstractType 'max_length' => $options['max_length'], 'pattern' => $options['pattern'], 'size' => null, - 'label' => $options['label'] ?: $this->humanize($form->getName()), + 'label' => $options['label'], 'multipart' => false, 'attr' => $options['attr'], 'label_attr' => $options['label_attr'], @@ -226,9 +226,4 @@ class FormType extends AbstractType { return 'form'; } - - private function humanize($text) - { - return ucfirst(trim(strtolower(preg_replace('/[_\s]+/', ' ', $text)))); - } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php index 3eadcc053b..03cce4cdc6 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php @@ -213,14 +213,6 @@ class FormTypeTest extends TypeTestCase $this->assertEquals('messages', $view['child']->getVar('translation_domain')); } - public function testPassDefaultLabelToView() - { - $form = $this->factory->createNamed('__test___field', 'form'); - $view = $form->createView(); - - $this->assertSame('Test field', $view->getVar('label')); - } - public function testPassLabelToView() { $form = $this->factory->createNamed('__test___field', 'form', null, array('label' => 'My label'));