Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ea7149a

Browse filesBrowse files
committed
minor #47203 [Form] ignore missing keys when mapping DateTime objects to uninitialized arrrays (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [Form] ignore missing keys when mapping DateTime objects to uninitialized arrrays | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | continues #47200 after merging changes up into the `5.4` branch Commits ------- 6ce5d52 ignore missing keys when mapping DateTime objects to uninitialized arrays
2 parents 3cf00b8 + 6ce5d52 commit ea7149a
Copy full SHA for ea7149a

File tree

Expand file treeCollapse file tree

3 files changed

+65
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+65
-1
lines changed

‎src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\Exception\AccessException;
1616
use Symfony\Component\Form\FormInterface;
1717
use Symfony\Component\PropertyAccess\Exception\AccessException as PropertyAccessException;
18+
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
1819
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1920
use Symfony\Component\PropertyAccess\PropertyAccess;
2021
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@@ -90,6 +91,10 @@ private function getPropertyValue($data, PropertyPathInterface $propertyPath)
9091
try {
9192
return $this->propertyAccessor->getValue($data, $propertyPath);
9293
} catch (PropertyAccessException $e) {
94+
if (\is_array($data) && $e instanceof NoSuchIndexException) {
95+
return null;
96+
}
97+
9398
if (!$e instanceof UninitializedPropertyException
9499
// For versions without UninitializedPropertyException check the exception message
95100
&& (class_exists(UninitializedPropertyException::class) || false === strpos($e->getMessage(), 'You should initialize it'))

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/CompoundFormTest.php
+29-1Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
1616
use Symfony\Component\Form\Exception\AlreadySubmittedException;
17+
use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
1718
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
1819
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
1920
use Symfony\Component\Form\Extension\Core\Type\DateType;
@@ -1079,7 +1080,10 @@ public function testFileUpload()
10791080
$this->assertNull($this->form->get('bar')->getData());
10801081
}
10811082

1082-
public function testMapDateTimeObjectsWithEmptyArrayData()
1083+
/**
1084+
* @group legacy
1085+
*/
1086+
public function testMapDateTimeObjectsWithEmptyArrayDataUsingPropertyPathMapper()
10831087
{
10841088
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
10851089
->enableExceptionOnInvalidIndex()
@@ -1103,6 +1107,30 @@ public function testMapDateTimeObjectsWithEmptyArrayData()
11031107
$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
11041108
}
11051109

1110+
public function testMapDateTimeObjectsWithEmptyArrayDataUsingDataMapper()
1111+
{
1112+
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
1113+
->enableExceptionOnInvalidIndex()
1114+
->getPropertyAccessor();
1115+
$form = $this->factory->createBuilder()
1116+
->setDataMapper(new DataMapper(new PropertyPathAccessor($propertyAccessor)))
1117+
->add('date', DateType::class, [
1118+
'auto_initialize' => false,
1119+
'format' => 'dd/MM/yyyy',
1120+
'html5' => false,
1121+
'model_timezone' => 'UTC',
1122+
'view_timezone' => 'UTC',
1123+
'widget' => 'single_text',
1124+
])
1125+
->getForm();
1126+
1127+
$form->submit([
1128+
'date' => '04/08/2022',
1129+
]);
1130+
1131+
$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
1132+
}
1133+
11061134
private function createForm(string $name = 'name', bool $compound = true): FormInterface
11071135
{
11081136
$builder = $this->getBuilder($name);

‎src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/DataMapperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/DataMapperTest.php
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
1616
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17+
use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
1718
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
19+
use Symfony\Component\Form\Extension\Core\Type\DateType;
1820
use Symfony\Component\Form\Form;
1921
use Symfony\Component\Form\FormConfigBuilder;
22+
use Symfony\Component\Form\FormFactoryBuilder;
2023
use Symfony\Component\Form\Tests\Fixtures\TypehintedPropertiesCar;
24+
use Symfony\Component\PropertyAccess\PropertyAccess;
2125
use Symfony\Component\PropertyAccess\PropertyPath;
2226

2327
class DataMapperTest extends TestCase
@@ -388,6 +392,33 @@ public function testMapFormsToDataUsingSetCallbackOption()
388392

389393
self::assertSame('Jane Doe', $person->myName());
390394
}
395+
396+
public function testMapFormsToDataMapsDateTimeInstanceToArrayIfNotSetBefore()
397+
{
398+
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
399+
->enableExceptionOnInvalidIndex()
400+
->getPropertyAccessor();
401+
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
402+
->enableExceptionOnInvalidIndex()
403+
->getPropertyAccessor();
404+
$form = (new FormFactoryBuilder())->getFormFactory()->createBuilder()
405+
->setDataMapper(new DataMapper(new PropertyPathAccessor($propertyAccessor)))
406+
->add('date', DateType::class, [
407+
'auto_initialize' => false,
408+
'format' => 'dd/MM/yyyy',
409+
'html5' => false,
410+
'model_timezone' => 'UTC',
411+
'view_timezone' => 'UTC',
412+
'widget' => 'single_text',
413+
])
414+
->getForm();
415+
416+
$form->submit([
417+
'date' => '04/08/2022',
418+
]);
419+
420+
$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
421+
}
391422
}
392423

393424
class SubmittedForm extends Form

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.