[Form] Fixed: The "date", "time" and "datetime" types can be initialized with \DateTime objects

This commit is contained in:
Bernhard Schussek 2012-02-07 11:10:02 +01:00
parent 88ef52d272
commit 3b1b57030b
7 changed files with 75 additions and 0 deletions

View File

@ -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,
);
}

View File

@ -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,
);
}

View File

@ -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,
);
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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(

View File

@ -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());
}
}