|
14 | 14 | use PHPUnit\Framework\TestCase; |
15 | 15 | use Symfony\Component\EventDispatcher\EventDispatcher; |
16 | 16 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 17 | +use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor; |
17 | 18 | use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper; |
| 19 | +use Symfony\Component\Form\Extension\Core\Type\DateType; |
18 | 20 | use Symfony\Component\Form\Form; |
19 | 21 | use Symfony\Component\Form\FormConfigBuilder; |
| 22 | +use Symfony\Component\Form\FormFactoryBuilder; |
20 | 23 | use Symfony\Component\Form\Tests\Fixtures\TypehintedPropertiesCar; |
| 24 | +use Symfony\Component\PropertyAccess\PropertyAccess; |
21 | 25 | use Symfony\Component\PropertyAccess\PropertyPath; |
22 | 26 |
|
23 | 27 | class DataMapperTest extends TestCase |
@@ -382,6 +386,33 @@ public function testMapFormsToDataUsingSetCallbackOption() |
382 | 386 |
|
383 | 387 | self::assertSame('Jane Doe', $person->myName()); |
384 | 388 | } |
| 389 | + |
| 390 | + public function testMapFormsToDataMapsDateTimeInstanceToArrayIfNotSetBefore() |
| 391 | + { |
| 392 | + $propertyAccessor = PropertyAccess::createPropertyAccessorBuilder() |
| 393 | + ->enableExceptionOnInvalidIndex() |
| 394 | + ->getPropertyAccessor(); |
| 395 | + $propertyAccessor = PropertyAccess::createPropertyAccessorBuilder() |
| 396 | + ->enableExceptionOnInvalidIndex() |
| 397 | + ->getPropertyAccessor(); |
| 398 | + $form = (new FormFactoryBuilder())->getFormFactory()->createBuilder() |
| 399 | + ->setDataMapper(new DataMapper(new PropertyPathAccessor($propertyAccessor))) |
| 400 | + ->add('date', DateType::class, [ |
| 401 | + 'auto_initialize' => false, |
| 402 | + 'format' => 'dd/MM/yyyy', |
| 403 | + 'html5' => false, |
| 404 | + 'model_timezone' => 'UTC', |
| 405 | + 'view_timezone' => 'UTC', |
| 406 | + 'widget' => 'single_text', |
| 407 | + ]) |
| 408 | + ->getForm(); |
| 409 | + |
| 410 | + $form->submit([ |
| 411 | + 'date' => '04/08/2022', |
| 412 | + ]); |
| 413 | + |
| 414 | + $this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData()); |
| 415 | + } |
385 | 416 | } |
386 | 417 |
|
387 | 418 | class SubmittedForm extends Form |
|
0 commit comments