Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6d79f68

Browse files
committed
ignore missing keys when mapping DateTime objects to uninitialized arrays
1 parent fcd9c2d commit 6d79f68

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-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: 26 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,30 @@ 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+
'view_timezone' => 'UTC',
1094+
'widget' => 'single_text',
1095+
])
1096+
->getForm();
1097+
1098+
$form->submit([
1099+
'date' => '04/08/2022',
1100+
]);
1101+
1102+
$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
1103+
}
1104+
10791105
private function createForm(string $name = 'name', bool $compound = true): FormInterface
10801106
{
10811107
$builder = $this->getBuilder($name);

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

Lines changed: 26 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,30 @@ public function provideDate()
362364
[new \DateTimeImmutable()],
363365
];
364366
}
367+
368+
public function testMapFormsToDataMapsDateTimeInstanceToArrayIfNotSetBefore()
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+
'view_timezone' => 'UTC',
381+
'widget' => 'single_text',
382+
])
383+
->getForm();
384+
385+
$form->submit([
386+
'date' => '04/08/2022',
387+
]);
388+
389+
$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
390+
}
365391
}
366392

367393
class SubmittedForm extends Form

0 commit comments

Comments
 (0)