From 0baa2e9cc53faad828c977902efb35a41e33ef16 Mon Sep 17 00:00:00 2001 From: Guilherme Blanco Date: Wed, 4 Feb 2015 20:45:10 +0000 Subject: [PATCH 1/2] Fixed a very weird edge case where empty data may not be a string and it throws a TransformationFailedException with message: 'Expected a string.'. --- src/Symfony/Component/Form/Form.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 7a18dcb80078a..ca490e4083ee9 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -612,6 +612,17 @@ public function submit($submittedData, $clearMissing = true) $emptyData = $emptyData($this, $viewData); } + // Treat false as NULL to support binding false to checkboxes. + // Don't convert NULL to a string here in order to determine later + // whether an empty value has been submitted or whether no value has + // been submitted at all. This is important for processing checkboxes + // and radio buttons with empty values. + if (false === $emptyData) { + $emptyData = null; + } elseif (is_scalar($emptyData)) { + $emptyData = (string) $emptyData; + } + $viewData = $emptyData; } From 85e88057d9b102f6fafd118ec401c4b539711a95 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 7 Mar 2016 13:26:07 +0100 Subject: [PATCH 2/2] add tests for previous commit to avoid regressions --- .../Extension/Core/Type/CheckboxTypeTest.php | 27 +++++++++++++++++++ .../Extension/Core/Type/IntegerTypeTest.php | 22 +++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php index 9437bd7eea655..8b39932de41fb 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php @@ -170,4 +170,31 @@ public function provideCustomModelTransformerData() array('unchecked', false), ); } + + public function testSubmitNullWithEmptyDataTrue() + { + $form = $this->factory->create('checkbox', null, array( + 'empty_data' => true, + )); + + $form->submit(null); + + $this->assertTrue($form->getData()); + $this->assertSame('1', $form->getViewData()); + } + + public function testSubmitNullWithEmptyDataFalse() + { + $form = $this->factory->create('checkbox', null, array( + 'empty_data' => false, + )); + + $form->submit(null); + + $this->assertFalse($form->getData()); + + $view = $form->createView(); + + $this->assertFalse($view->vars['checked']); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/IntegerTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/IntegerTypeTest.php index 85f91ff18126d..ee11a9a1c1bfc 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/IntegerTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/IntegerTypeTest.php @@ -32,4 +32,26 @@ public function testSubmitCastsToInteger() $this->assertSame(1, $form->getData()); $this->assertSame('1', $form->getViewData()); } + + public function testSubmitNull() + { + $form = $this->factory->create('integer'); + + $form->submit(null); + + $this->assertNull($form->getData()); + $this->assertSame('', $form->getViewData()); + } + + public function testSubmitNullWithEmptyData() + { + $form = $this->factory->create('integer', null, array( + 'empty_data' => 1, + )); + + $form->submit(null); + + $this->assertSame(1, $form->getData()); + $this->assertSame('1', $form->getViewData()); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy