Browse Source

merged branch kriswallsmith/form/checkbox-view (PR #2661)

Commits
-------

79ae3fc [Form] fixed radio and checkbox when data is not bool

Discussion
----------

[Form] fixed checkbox view

The checkbox view was being built based on app data, not client data. This fixes it.

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -

---------------------------------------------------------------------------

by fabpot at 2011/11/16 13:31:09 -0800

`RadioType` suffers from the same problem, no?

---------------------------------------------------------------------------

by kriswallsmith at 2011/11/16 13:32:50 -0800

Yeah, I'll fix that too.

---------------------------------------------------------------------------

by kriswallsmith at 2011/11/16 13:43:29 -0800

Updated to include `RadioType`.
Fabien Potencier 13 years ago
parent
commit
12299c4de9

+ 1 - 1
src/Symfony/Component/Form/Extension/Core/Type/CheckboxType.php

@@ -37,7 +37,7 @@ class CheckboxType extends AbstractType
     {
         $view
             ->set('value', $form->getAttribute('value'))
-            ->set('checked', (Boolean) $form->getData())
+            ->set('checked', (Boolean) $form->getClientData())
         ;
     }
 

+ 1 - 1
src/Symfony/Component/Form/Extension/Core/Type/RadioType.php

@@ -37,7 +37,7 @@ class RadioType extends AbstractType
     {
         $view
             ->set('value', $form->getAttribute('value'))
-            ->set('checked', (Boolean) $form->getData())
+            ->set('checked', (Boolean) $form->getClientData())
         ;
 
         if ($view->hasParent()) {

+ 37 - 0
tests/Symfony/Tests/Component/Form/Extension/Core/Type/CheckboxTypeTest.php

@@ -11,6 +11,8 @@
 
 namespace Symfony\Tests\Component\Form\Extension\Core\Type;
 
+use Symfony\Component\Form\CallbackTransformer;
+
 class CheckboxTypeTest extends TypeTestCase
 {
     public function testPassValueToView()
@@ -38,4 +40,39 @@ class CheckboxTypeTest extends TypeTestCase
 
         $this->assertFalse($view->get('checked'));
     }
+
+    /**
+     * @dataProvider proviceTransformedData
+     */
+    public function testTransformedData($data, $expected)
+    {
+        // present a binary status field as a checkbox
+        $transformer = new CallbackTransformer(
+            function ($value)
+            {
+                return 'expedited' == $value;
+            },
+            function ($value)
+            {
+                return $value ? 'expedited' : 'standard';
+            }
+        );
+
+        $form = $this->builder
+            ->create('expedited_shipping', 'checkbox')
+            ->prependClientTransformer($transformer)
+            ->getForm();
+        $form->setData($data);
+        $view = $form->createView();
+
+        $this->assertEquals($expected, $view->get('checked'));
+    }
+
+    public function proviceTransformedData()
+    {
+        return array(
+            array('expedited', true),
+            array('standard', false),
+        );
+    }
 }