From 52755ba26d59057bc5642fb97df9454457d06df9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Vasseur?= Date: Mon, 29 Jun 2015 19:01:51 +0200 Subject: [PATCH] Fix choice translation domain for expanded choice widget --- .../views/Form/bootstrap_3_layout.html.twig | 4 +- .../views/Form/form_div_layout.html.twig | 4 +- .../Form/choice_widget_expanded.html.php | 2 +- .../Resources/views/Form/form_label.html.php | 2 +- .../Tests/AbstractBootstrap3LayoutTest.php | 105 ++++++++++++++++++ .../Form/Tests/AbstractLayoutTest.php | 85 ++++++++++++++ 6 files changed, 197 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig index 6c92dc8b6b02..7405612a9b8d 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig @@ -98,6 +98,7 @@ {%- for child in form %} {{- form_widget(child, { parent_label_class: label_attr.class|default(''), + translation_domain: choice_translation_domain, }) -}} {% endfor -%} @@ -106,6 +107,7 @@ {%- for child in form %} {{- form_widget(child, { parent_label_class: label_attr.class|default(''), + translation_domain: choice_translation_domain, }) -}} {% endfor -%} @@ -169,7 +171,7 @@ {% endif %} {{- widget|raw -}} - {{- label is not sameas(false) ? label|trans({}, translation_domain) -}} + {{- label is not sameas(false) ? (translation_domain is sameas(false) ? label : label|trans({}, translation_domain)) -}} {% endif %} {% endblock checkbox_radio_label %} 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 6b315f464040..48a432e41a4c 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 @@ -46,7 +46,7 @@
{%- for child in form %} {{- form_widget(child) -}} - {{- form_label(child) -}} + {{- form_label(child, null, {translation_domain: choice_translation_domain}) -}} {% endfor -%}
{%- endblock choice_widget_expanded -%} @@ -220,7 +220,7 @@ {% set label = name|humanize %} {%- endif -%} {%- endif -%} - {{ label|trans({}, translation_domain) }} + {{ translation_domain is sameas(false) ? label : label|trans({}, translation_domain) }} {%- endif -%} {%- endblock form_label -%} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_expanded.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_expanded.html.php index 64ae7dbc953c..2c426872071f 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_expanded.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_expanded.html.php @@ -1,6 +1,6 @@
block($form, 'widget_container_attributes') ?>> widget($child) ?> - label($child) ?> + label($child, null, array('translation_domain' => $choice_translation_domain)) ?>
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 27bba27c2b5c..7a51b2ce7b4f 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 @@ -4,5 +4,5 @@ $name, '%id%' => $id)) : $view['form']->humanize($name); } ?> - + diff --git a/src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php index da506025d9c4..44f08b4fc12b 100644 --- a/src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php @@ -231,6 +231,29 @@ public function testSingleChoice() ); } + public function testSingleChoiceWithoutTranslation() + { + $form = $this->factory->createNamed('name', 'choice', '&a', array( + 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'), + 'multiple' => false, + 'expanded' => false, + 'choice_translation_domain' => false, + )); + + $this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')), +'/select + [@name="name"] + [@class="my&class form-control"] + [not(@required)] + [ + ./option[@value="&a"][@selected="selected"][.="Choice&A"] + /following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"] + ] + [count(./option)=2] +' + ); + } + public function testSingleChoiceAttributes() { $form = $this->factory->createNamed('name', 'choice', '&a', array( @@ -629,6 +652,42 @@ public function testSingleChoiceExpanded() ); } + public function testSingleChoiceExpandedWithoutTranslation() + { + $form = $this->factory->createNamed('name', 'choice', '&a', array( + 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'), + 'multiple' => false, + 'expanded' => true, + 'choice_translation_domain' => false, + )); + + $this->assertWidgetMatchesXpath($form->createView(), array(), +'/div + [ + ./div + [@class="radio"] + [ + ./label + [.="Choice&A"] + [ + ./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked] + ] + ] + /following-sibling::div + [@class="radio"] + [ + ./label + [.="Choice&B"] + [ + ./input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)] + ] + ] + /following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"] + ] +' + ); + } + public function testSingleChoiceExpandedAttributes() { $form = $this->factory->createNamed('name', 'choice', '&a', array( @@ -792,6 +851,52 @@ public function testMultipleChoiceExpanded() ); } + public function testMultipleChoiceExpandedWithoutTranslation() + { + $form = $this->factory->createNamed('name', 'choice', array('&a', '&c'), array( + 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B', '&c' => 'Choice&C'), + 'multiple' => true, + 'expanded' => true, + 'required' => true, + 'choice_translation_domain' => false, + )); + + $this->assertWidgetMatchesXpath($form->createView(), array(), +'/div + [ + ./div + [@class="checkbox"] + [ + ./label + [.="Choice&A"] + [ + ./input[@type="checkbox"][@name="name[]"][@id="name_0"][@checked][not(@required)] + ] + ] + /following-sibling::div + [@class="checkbox"] + [ + ./label + [.="Choice&B"] + [ + ./input[@type="checkbox"][@name="name[]"][@id="name_1"][not(@checked)][not(@required)] + ] + ] + /following-sibling::div + [@class="checkbox"] + [ + ./label + [.="Choice&C"] + [ + ./input[@type="checkbox"][@name="name[]"][@id="name_2"][@checked][not(@required)] + ] + ] + /following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"] + ] +' + ); + } + public function testMultipleChoiceExpandedAttributes() { $form = $this->factory->createNamed('name', 'choice', array('&a', '&c'), array( diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index 8589f35904ec..e4b82063faac 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -168,6 +168,20 @@ public function testLabel() ); } + public function testLabelWithoutTranslation() + { + $form = $this->factory->createNamed('name', 'text', null, array( + 'translation_domain' => false, + )); + + $this->assertMatchesXpath($this->renderLabel($form->createView()), +'/label + [@for="name"] + [.="Name"] +' + ); + } + public function testLabelOnForm() { $form = $this->factory->createNamed('name', 'date'); @@ -513,6 +527,28 @@ public function testSingleChoice() ); } + public function testSingleChoiceWithoutTranslation() + { + $form = $this->factory->createNamed('name', 'choice', '&a', array( + 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'), + 'multiple' => false, + 'expanded' => false, + 'choice_translation_domain' => false, + )); + + $this->assertWidgetMatchesXpath($form->createView(), array(), +'/select + [@name="name"] + [not(@required)] + [ + ./option[@value="&a"][@selected="selected"][.="Choice&A"] + /following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"] + ] + [count(./option)=2] +' + ); + } + public function testSingleChoiceAttributes() { $form = $this->factory->createNamed('name', 'choice', '&a', array( @@ -889,6 +925,29 @@ public function testSingleChoiceExpanded() ); } + public function testSingleChoiceExpandedWithoutTranslation() + { + $form = $this->factory->createNamed('name', 'choice', '&a', array( + 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'), + 'multiple' => false, + 'expanded' => true, + 'choice_translation_domain' => false, + )); + + $this->assertWidgetMatchesXpath($form->createView(), array(), +'/div + [ + ./input[@type="radio"][@name="name"][@id="name_0"][@value="&a"][@checked] + /following-sibling::label[@for="name_0"][.="Choice&A"] + /following-sibling::input[@type="radio"][@name="name"][@id="name_1"][@value="&b"][not(@checked)] + /following-sibling::label[@for="name_1"][.="Choice&B"] + /following-sibling::input[@type="hidden"][@id="name__token"] + ] + [count(./input)=3] +' + ); + } + public function testSingleChoiceExpandedAttributes() { $form = $this->factory->createNamed('name', 'choice', '&a', array( @@ -986,6 +1045,32 @@ public function testMultipleChoiceExpanded() ); } + public function testMultipleChoiceExpandedWithoutTranslation() + { + $form = $this->factory->createNamed('name', 'choice', array('&a', '&c'), array( + 'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B', '&c' => 'Choice&C'), + 'multiple' => true, + 'expanded' => true, + 'required' => true, + 'choice_translation_domain' => false, + )); + + $this->assertWidgetMatchesXpath($form->createView(), array(), +'/div + [ + ./input[@type="checkbox"][@name="name[]"][@id="name_0"][@checked][not(@required)] + /following-sibling::label[@for="name_0"][.="Choice&A"] + /following-sibling::input[@type="checkbox"][@name="name[]"][@id="name_1"][not(@checked)][not(@required)] + /following-sibling::label[@for="name_1"][.="Choice&B"] + /following-sibling::input[@type="checkbox"][@name="name[]"][@id="name_2"][@checked][not(@required)] + /following-sibling::label[@for="name_2"][.="Choice&C"] + /following-sibling::input[@type="hidden"][@id="name__token"] + ] + [count(./input)=4] +' + ); + } + public function testMultipleChoiceExpandedAttributes() { $form = $this->factory->createNamed('name', 'choice', array('&a', '&c'), array(