Pārlūkot izejas kodu

[Form] Fixed ChoiceField::isChoiceSelected() to differentiate between zero and empty

Bernhard Schussek 14 gadi atpakaļ
vecāks
revīzija
40acc6ac79

+ 1 - 1
src/Symfony/Component/Form/ChoiceField.php

@@ -180,7 +180,7 @@ class ChoiceField extends HybridField
 
     public function isChoiceSelected($choice)
     {
-        return in_array($choice, (array) $this->getDisplayedData());
+        return in_array((string) $choice, (array) $this->getDisplayedData(), true);
     }
 
     public function isMultipleChoice()

+ 3 - 1
src/Symfony/Component/Form/Field.php

@@ -494,7 +494,9 @@ class Field extends Configurable implements FieldInterface
     protected function transform($value)
     {
         if (null === $this->valueTransformer) {
-            return null === $value ? '' : $value;
+            // Scalar values should always be converted to strings to
+            // facilitate differentiation between empty ("") and zero (0).
+            return null === $value || is_scalar($value) ? (string)$value : $value;
         }
         return $this->valueTransformer->transform($value);
     }

+ 56 - 0
tests/Symfony/Tests/Component/Form/ChoiceFieldTest.php

@@ -46,6 +46,62 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase
         4 => 'Roman',
     );
 
+    public function testIsChoiceSelectedDifferentiatesBetweenZeroAndEmpty_integerZero()
+    {
+        $field = new ChoiceField('name', array(
+            'choices' => array(
+                0 => 'Foo',
+                '' => 'Bar',
+            )
+        ));
+
+        $field->submit(0);
+
+        $this->assertTrue($field->isChoiceSelected(0));
+        $this->assertTrue($field->isChoiceSelected('0'));
+        $this->assertFalse($field->isChoiceSelected(''));
+
+        $field->submit('0');
+
+        $this->assertTrue($field->isChoiceSelected(0));
+        $this->assertTrue($field->isChoiceSelected('0'));
+        $this->assertFalse($field->isChoiceSelected(''));
+
+        $field->submit('');
+
+        $this->assertFalse($field->isChoiceSelected(0));
+        $this->assertFalse($field->isChoiceSelected('0'));
+        $this->assertTrue($field->isChoiceSelected(''));
+    }
+
+    public function testIsChoiceSelectedDifferentiatesBetweenZeroAndEmpty_stringZero()
+    {
+        $field = new ChoiceField('name', array(
+            'choices' => array(
+                '0' => 'Foo',
+                '' => 'Bar',
+            )
+        ));
+
+        $field->submit(0);
+
+        $this->assertTrue($field->isChoiceSelected(0));
+        $this->assertTrue($field->isChoiceSelected('0'));
+        $this->assertFalse($field->isChoiceSelected(''));
+
+        $field->submit('0');
+
+        $this->assertTrue($field->isChoiceSelected(0));
+        $this->assertTrue($field->isChoiceSelected('0'));
+        $this->assertFalse($field->isChoiceSelected(''));
+
+        $field->submit('');
+
+        $this->assertFalse($field->isChoiceSelected(0));
+        $this->assertFalse($field->isChoiceSelected('0'));
+        $this->assertTrue($field->isChoiceSelected(''));
+    }
+
     /**
      * @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
      */

+ 8 - 0
tests/Symfony/Tests/Component/Form/FieldTest.php

@@ -220,6 +220,14 @@ class FieldTest extends \PHPUnit_Framework_TestCase
         $this->assertSame('', $this->field->getDisplayedData());
     }
 
+    public function testValuesAreTransformedCorrectlyIfNotNull_noValueTransformer()
+    {
+        $this->field->setData(123);
+
+        $this->assertSame(123, $this->field->getData());
+        $this->assertSame('123', $this->field->getDisplayedData());
+    }
+
     public function testSubmittedValuesAreTransformedCorrectly()
     {
         $valueTransformer = $this->createMockTransformer();