Skip to content

Commit fd55e23

Browse files
committed
do not fail when mapping DateTime objects to uninitialized arrays
1 parent fcd9c2d commit fd55e23

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Form\DataMapperInterface;
1515
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1616
use Symfony\Component\PropertyAccess\Exception\AccessException;
17+
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
1718
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1819
use Symfony\Component\PropertyAccess\PropertyAccess;
1920
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@@ -96,6 +97,10 @@ private function getPropertyValue($data, $propertyPath)
9697
try {
9798
return $this->propertyAccessor->getValue($data, $propertyPath);
9899
} catch (AccessException $e) {
100+
if (is_array($data) && $e instanceof NoSuchIndexException) {
101+
return null;
102+
}
103+
99104
if (!$e instanceof UninitializedPropertyException
100105
// For versions without UninitializedPropertyException check the exception message
101106
&& (class_exists(UninitializedPropertyException::class) || !str_contains($e->getMessage(), 'You should initialize it'))

src/Symfony/Component/Form/Tests/CompoundFormTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
1616
use Symfony\Component\Form\Exception\AlreadySubmittedException;
1717
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
18+
use Symfony\Component\Form\Extension\Core\Type\DateType;
1819
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
1920
use Symfony\Component\Form\Extension\Core\Type\TextType;
2021
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
@@ -36,6 +37,7 @@
3637
use Symfony\Component\Form\Tests\Fixtures\Map;
3738
use Symfony\Component\HttpFoundation\File\UploadedFile;
3839
use Symfony\Component\HttpFoundation\Request;
40+
use Symfony\Component\PropertyAccess\PropertyAccess;
3941

4042
class CompoundFormTest extends TestCase
4143
{
@@ -1076,6 +1078,29 @@ public function testFileUpload()
10761078
$this->assertNull($this->form->get('bar')->getData());
10771079
}
10781080

1081+
public function testMapDateTimeObjectsWithEmptyArrayData()
1082+
{
1083+
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
1084+
->enableExceptionOnInvalidIndex()
1085+
->getPropertyAccessor();
1086+
$form = $this->factory->createBuilder()
1087+
->setDataMapper(new PropertyPathMapper($propertyAccessor))
1088+
->add('date', DateType::class, [
1089+
'auto_initialize' => false,
1090+
'format' => 'dd/MM/yyyy',
1091+
'html5' => false,
1092+
'model_timezone' => 'UTC',
1093+
'widget' => 'single_text',
1094+
])
1095+
->getForm();
1096+
1097+
$form->submit([
1098+
'date' => '04/08/2022',
1099+
]);
1100+
1101+
$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
1102+
}
1103+
10791104
private function createForm(string $name = 'name', bool $compound = true): FormInterface
10801105
{
10811106
$builder = $this->getBuilder($name);

src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
1616
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1717
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
18+
use Symfony\Component\Form\Extension\Core\Type\DateType;
1819
use Symfony\Component\Form\Form;
1920
use Symfony\Component\Form\FormConfigBuilder;
21+
use Symfony\Component\Form\FormFactoryBuilder;
2022
use Symfony\Component\Form\Tests\Fixtures\TypehintedPropertiesCar;
2123
use Symfony\Component\PropertyAccess\PropertyAccess;
2224
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@@ -362,6 +364,29 @@ public function provideDate()
362364
[new \DateTimeImmutable()],
363365
];
364366
}
367+
368+
public function testMapDateTimeObjectsWithEmptyArrayData()
369+
{
370+
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
371+
->enableExceptionOnInvalidIndex()
372+
->getPropertyAccessor();
373+
$form = (new FormFactoryBuilder())->getFormFactory()->createBuilder()
374+
->setDataMapper(new PropertyPathMapper($propertyAccessor))
375+
->add('date', DateType::class, [
376+
'auto_initialize' => false,
377+
'format' => 'dd/MM/yyyy',
378+
'html5' => false,
379+
'model_timezone' => 'UTC',
380+
'widget' => 'single_text',
381+
])
382+
->getForm();
383+
384+
$form->submit([
385+
'date' => '04/08/2022',
386+
]);
387+
388+
$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
389+
}
365390
}
366391

367392
class SubmittedForm extends Form

0 commit comments

Comments
 (0)
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