Skip to content

Commit 429f193

Browse files
committed
Merge branch '4.4' into 5.4
* 4.4: ignore missing keys when mapping DateTime objects to uninitialized arrays fix writes to static $kernel property [Validator] Hint that `egulias/email-validator` needs to be installed for strict mode as default config
2 parents 1c156d7 + 5f91e32 commit 429f193

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

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;
@@ -100,6 +101,10 @@ private function getPropertyValue($data, $propertyPath)
100101
try {
101102
return $this->propertyAccessor->getValue($data, $propertyPath);
102103
} catch (AccessException $e) {
104+
if (\is_array($data) && $e instanceof NoSuchIndexException) {
105+
return null;
106+
}
107+
103108
if (!$e instanceof UninitializedPropertyException
104109
// For versions without UninitializedPropertyException check the exception message
105110
&& (class_exists(UninitializedPropertyException::class) || !str_contains($e->getMessage(), 'You should initialize it'))

Tests/CompoundFormTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
1616
use Symfony\Component\Form\Exception\AlreadySubmittedException;
1717
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
18+
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
19+
use Symfony\Component\Form\Extension\Core\Type\DateType;
1820
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
1921
use Symfony\Component\Form\Extension\Core\Type\TextType;
2022
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
@@ -36,6 +38,7 @@
3638
use Symfony\Component\Form\Tests\Fixtures\Map;
3739
use Symfony\Component\HttpFoundation\File\UploadedFile;
3840
use Symfony\Component\HttpFoundation\Request;
41+
use Symfony\Component\PropertyAccess\PropertyAccess;
3942

4043
class CompoundFormTest extends TestCase
4144
{
@@ -1076,6 +1079,30 @@ public function testFileUpload()
10761079
$this->assertNull($this->form->get('bar')->getData());
10771080
}
10781081

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

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;
@@ -365,4 +367,28 @@ public function provideDate()
365367
[new \DateTimeImmutable()],
366368
];
367369
}
370+
371+
public function testMapFormsToDataMapsDateTimeInstanceToArrayIfNotSetBefore()
372+
{
373+
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
374+
->enableExceptionOnInvalidIndex()
375+
->getPropertyAccessor();
376+
$form = (new FormFactoryBuilder())->getFormFactory()->createBuilder()
377+
->setDataMapper(new PropertyPathMapper($propertyAccessor))
378+
->add('date', DateType::class, [
379+
'auto_initialize' => false,
380+
'format' => 'dd/MM/yyyy',
381+
'html5' => false,
382+
'model_timezone' => 'UTC',
383+
'view_timezone' => 'UTC',
384+
'widget' => 'single_text',
385+
])
386+
->getForm();
387+
388+
$form->submit([
389+
'date' => '04/08/2022',
390+
]);
391+
392+
$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
393+
}
368394
}

0 commit comments

Comments
 (0)