Skip to content

Commit 1d9bd70

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: fix dispatch signal event check for compatibility with the contract interface ignore missing keys when mapping DateTime objects to uninitialized arrays
2 parents c0e95ef + d8c5cc9 commit 1d9bd70

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

Extension/Core/DataAccessor/PropertyPathAccessor.php

Lines 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(object|array $data, PropertyPathInterface $pro
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'))

Tests/CompoundFormTest.php

Lines 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);

Tests/Extension/Core/DataMapper/DataMapperTest.php

Lines 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
@@ -382,6 +386,33 @@ public function testMapFormsToDataUsingSetCallbackOption()
382386

383387
self::assertSame('Jane Doe', $person->myName());
384388
}
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+
}
385416
}
386417

387418
class SubmittedForm extends Form

0 commit comments

Comments
 (0)