From e4939849ab6e585d9a04d876e246109e140f27e7 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Mon, 22 Apr 2013 13:33:42 +0100 Subject: [PATCH 1/2] [Form] Allowed binding false to a checkbox. --- src/Symfony/Component/Form/Form.php | 5 ++++- .../Extension/Core/Type/CheckboxTypeTest.php | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index a7a057dd09bc8..1b3b0f3120ebd 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -488,11 +488,14 @@ public function bind($submittedData) $this->setData($this->config->getData()); } + // 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 (is_scalar($submittedData)) { + if (false === $submittedData) { + $submittedData = null; + } elseif (is_scalar($submittedData)) { $submittedData = (string) $submittedData; } 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 304958f768e99..d4b7e8c08663d 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CheckboxTypeTest.php @@ -105,6 +105,28 @@ public function testBindWithEmptyValueUnchecked() $this->assertNull($form->getViewData()); } + public function testBindWithEmptyValueAndFalseUnchecked() + { + $form = $this->factory->create('checkbox', null, array( + 'value' => '', + )); + $form->bind(false); + + $this->assertFalse($form->getData()); + $this->assertNull($form->getViewData()); + } + + public function testBindWithEmptyValueAndTrueChecked() + { + $form = $this->factory->create('checkbox', null, array( + 'value' => '', + )); + $form->bind(true); + + $this->assertTrue($form->getData()); + $this->assertSame('', $form->getViewData()); + } + /** * @dataProvider provideTransformedData */ From 24ef8d233630da8b33d3b5a057f7df37f4039b52 Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Mon, 22 Apr 2013 14:11:14 +0100 Subject: [PATCH 2/2] [Form] Added a SimpleFormTest test case. --- .../Component/Form/Tests/SimpleFormTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Symfony/Component/Form/Tests/SimpleFormTest.php b/src/Symfony/Component/Form/Tests/SimpleFormTest.php index 09dea71d250cb..c80a03005ef7b 100644 --- a/src/Symfony/Component/Form/Tests/SimpleFormTest.php +++ b/src/Symfony/Component/Form/Tests/SimpleFormTest.php @@ -96,6 +96,28 @@ public function testDataIsInitializedFromBind() $form->bind('foobar'); } + // https://github.com/symfony/symfony/pull/7789 + public function testFalseIsConvertedToNull() + { + $mock = $this->getMockBuilder('\stdClass') + ->setMethods(array('preBind')) + ->getMock(); + $mock->expects($this->once()) + ->method('preBind') + ->with($this->callback(function ($event) { + return null === $event->getData(); + })); + + $config = new FormConfigBuilder('name', null, $this->dispatcher); + $config->addEventListener(FormEvents::PRE_BIND, array($mock, 'preBind')); + $form = new Form($config); + + $form->bind(false); + + $this->assertTrue($form->isValid()); + $this->assertNull($form->getData()); + } + /** * @expectedException \Symfony\Component\Form\Exception\AlreadyBoundException */ 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