From 3b1b57030bbe92bd36d4fab974598c9b1f61e0dc Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Tue, 7 Feb 2012 11:10:02 +0100 Subject: [PATCH] [Form] Fixed: The "date", "time" and "datetime" types can be initialized with \DateTime objects --- .../Form/Extension/Core/Type/DateTimeType.php | 5 +++ .../Form/Extension/Core/Type/DateType.php | 5 +++ .../Form/Extension/Core/Type/TimeType.php | 5 +++ .../Extension/Core/Type/DateTimeTypeTest.php | 8 +++++ .../Form/Extension/Core/Type/DateTypeTest.php | 8 +++++ .../Extension/Core/Type/FieldTypeTest.php | 36 +++++++++++++++++++ .../Form/Extension/Core/Type/TimeTypeTest.php | 8 +++++ 7 files changed, 75 insertions(+) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php index 7906a2ba22..ba6846007e 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php @@ -158,6 +158,11 @@ class DateTimeType extends AbstractType 'widget' => null, // This will overwrite "empty_value" child options 'empty_value' => null, + // If initialized with a \DateTime object, FieldType initializes + // this option to "\DateTime". Since the internal, normalized + // representation is not \DateTime, but an array, we need to unset + // this option. + 'data_class' => null, ); } diff --git a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php index b8411fd6a3..c477aa39e2 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/DateType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/DateType.php @@ -183,6 +183,11 @@ class DateType extends AbstractType // them like immutable value objects 'by_reference' => false, 'error_bubbling' => false, + // If initialized with a \DateTime object, FieldType initializes + // this option to "\DateTime". Since the internal, normalized + // representation is not \DateTime, but an array, we need to unset + // this option. + 'data_class' => null, ); } diff --git a/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php index 81465cb547..9ec85cd24e 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/TimeType.php @@ -156,6 +156,11 @@ class TimeType extends AbstractType // them like immutable value objects 'by_reference' => false, 'error_bubbling' => false, + // If initialized with a \DateTime object, FieldType initializes + // this option to "\DateTime". Since the internal, normalized + // representation is not \DateTime, but an array, we need to unset + // this option. + 'data_class' => null, ); } diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTimeTypeTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTimeTypeTest.php index 27f3108d9a..2cd18ad4ef 100644 --- a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTimeTypeTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTimeTypeTest.php @@ -258,4 +258,12 @@ class DateTimeTypeTest extends LocalizedTestCase $this->assertEquals(array(new FormError('Customized invalid message', array())), $form['date']->getErrors()); $this->assertEquals(array(new FormError('Customized invalid message', array())), $form['time']->getErrors()); } + + // Bug fix + public function testInitializeWithDateTime() + { + // Throws an exception if "data_class" option is not explicitely set + // to null in the type + $form = $this->factory->create('datetime', new \DateTime()); + } } diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTypeTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTypeTest.php index 1be7affc70..8bd78fcf94 100644 --- a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTypeTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTypeTest.php @@ -530,4 +530,12 @@ class DateTypeTest extends LocalizedTestCase $this->assertSame('single_text', $view->get('widget')); } + + // Bug fix + public function testInitializeWithDateTime() + { + // Throws an exception if "data_class" option is not explicitely set + // to null in the type + $form = $this->factory->create('date', new \DateTime()); + } } diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php index e170628351..036f829aa7 100644 --- a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php @@ -221,6 +221,42 @@ class FieldTypeTest extends TypeTestCase $this->assertEquals($author, $form->getData()); } + public function testBindWithEmptyDataCreatesObjectIfInitiallyBoundWithObject() + { + $form = $this->factory->create('form', null, array( + // data class is inferred from the passed object + 'data' => new Author(), + 'required' => false, + )); + $form->add($this->factory->createNamed('field', 'firstName')); + $form->add($this->factory->createNamed('field', 'lastName')); + + $form->setData(null); + // partially empty, still an object is created + $form->bind(array('firstName' => 'Bernhard', 'lastName' => '')); + + $author = new Author(); + $author->firstName = 'Bernhard'; + $author->setLastName(''); + + $this->assertEquals($author, $form->getData()); + } + + public function testBindWithEmptyDataDoesNotCreateObjectIfDataClassIsNull() + { + $form = $this->factory->create('form', null, array( + 'data' => new Author(), + 'data_class' => null, + 'required' => false, + )); + $form->add($this->factory->createNamed('field', 'firstName')); + + $form->setData(null); + $form->bind(array('firstName' => 'Bernhard')); + + $this->assertSame(array('firstName' => 'Bernhard'), $form->getData()); + } + public function testBindEmptyWithEmptyDataCreatesNoObjectIfNotRequired() { $form = $this->factory->create('form', null, array( diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/TimeTypeTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/TimeTypeTest.php index 34676ddf19..89e250790a 100644 --- a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/TimeTypeTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/TimeTypeTest.php @@ -401,4 +401,12 @@ class TimeTypeTest extends LocalizedTestCase $this->assertTrue($form->isPartiallyFilled()); } + + // Bug fix + public function testInitializeWithDateTime() + { + // Throws an exception if "data_class" option is not explicitely set + // to null in the type + $form = $this->factory->create('time', new \DateTime()); + } }