Explorar o código

removed separator of choice widget when the separator is null

Fabien Potencier %!s(int64=13) %!d(string=hai) anos
pai
achega
d429594afb

+ 1 - 1
src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

@@ -57,7 +57,7 @@
         {% if preferred_choices|length > 0 %}
             {% set options = preferred_choices %}
             {{ block('widget_choice_options') }}
-            {% if choices|length > 0 %}
+            {% if choices|length > 0 and separator is not none %}
                 <option disabled="disabled">{{ separator }}</option>
             {% endif %}
         {% endif %}

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget.html.php

@@ -13,7 +13,7 @@
         <?php if (null !== $empty_value): ?><option value=""><?php echo $view->escape($view['translator']->trans($empty_value)) ?></option><?php endif; ?>
         <?php if (count($preferred_choices) > 0): ?>
             <?php echo $view['form']->renderBlock('choice_options', array('options' => $preferred_choices)) ?>
-            <?php if (count($choices) > 0): ?>
+            <?php if (count($choices) > 0 && null !== $separator): ?>
                 <option disabled="disabled"><?php echo $separator ?></option>
             <?php endif ?>
         <?php endif ?>

+ 48 - 0
tests/Symfony/Tests/Component/Form/AbstractLayoutTest.php

@@ -383,6 +383,54 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
         );
     }
 
+
+    public function testSingleChoiceWithPreferredAndNoSeparator()
+    {
+        $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
+            'property_path' => 'name',
+            'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
+            'preferred_choices' => array('&b'),
+            'multiple' => false,
+            'expanded' => false,
+        ));
+
+        $this->assertWidgetMatchesXpath($form->createView(), array('separator' => null),
+'/select
+    [@name="na&me"]
+    [@required="required"]
+    [
+        ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
+        /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+    ]
+    [count(./option)=2]
+'
+        );
+    }
+
+    public function testSingleChoiceWithPreferredAndBlankSeparator()
+    {
+        $form = $this->factory->createNamed('choice', 'na&me', '&a', array(
+            'property_path' => 'name',
+            'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
+            'preferred_choices' => array('&b'),
+            'multiple' => false,
+            'expanded' => false,
+        ));
+
+        $this->assertWidgetMatchesXpath($form->createView(), array('separator' => ''),
+'/select
+    [@name="na&me"]
+    [@required="required"]
+    [
+        ./option[@value="&b"][not(@selected)][.="[trans]Choice&B[/trans]"]
+        /following-sibling::option[@disabled="disabled"][not(@selected)][.=""]
+        /following-sibling::option[@value="&a"][@selected="selected"][.="[trans]Choice&A[/trans]"]
+    ]
+    [count(./option)=3]
+'
+        );
+    }
+
     public function testChoiceWithOnlyPreferred()
     {
         $form = $this->factory->createNamed('choice', 'na&me', '&a', array(