Browse Source

[Form] Fixed single expanded choice type to set checked attribute when passed boolean value

Katsuhiro OGAWA 14 years ago
parent
commit
5893874ed5

+ 6 - 2
src/Symfony/Component/Form/Extension/Core/DataTransformer/ScalarToBooleanChoicesTransformer.php

@@ -51,6 +51,10 @@ class ScalarToBooleanChoicesTransformer implements DataTransformerInterface
             throw new UnexpectedTypeException($value, 'scalar');
         }
 
+        if (is_bool($value) || null === $value) {
+            $value = (int)$value;
+        }
+
         try {
             $choices = $this->choiceList->getChoices();
         } catch (\Exception $e) {
@@ -58,7 +62,7 @@ class ScalarToBooleanChoicesTransformer implements DataTransformerInterface
         }
 
         foreach (array_keys($choices) as $key) {
-            $choices[$key] = $key === $value;
+            $choices[$key] = (string)$key === (string)$value;
         }
 
         return $choices;
@@ -91,4 +95,4 @@ class ScalarToBooleanChoicesTransformer implements DataTransformerInterface
 
         return null;
     }
-}
+}

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

@@ -479,6 +479,28 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase
         );
     }
 
+    public function testSingleChoiceExpandedWithBooleanValue()
+    {
+        $form = $this->factory->createNamed('choice', 'na&me', true, array(
+            'property_path' => 'name',
+            'choices' => array('1' => 'Choice&A', '0' => 'Choice&B'),
+            'multiple' => false,
+            'expanded' => true,
+        ));
+
+        $this->assertWidgetMatchesXpath($form->createView(), array(),
+'/div
+    [
+        ./input[@type="radio"][@name="na&me"][@id="na&me_1"][@checked]
+        /following-sibling::label[@for="na&me_1"][.="[trans]Choice&A[/trans]"]
+        /following-sibling::input[@type="radio"][@name="na&me"][@id="na&me_0"][not(@checked)]
+        /following-sibling::label[@for="na&me_0"][.="[trans]Choice&B[/trans]"]
+    ]
+    [count(./input)=2]
+'
+        );
+    }
+
     public function testMultipleChoiceExpanded()
     {
         $form = $this->factory->createNamed('choice', 'na&me', array('&a', '&c'), array(