Skip to content

[Form] ignore missing keys when mapping DateTime objects to uninitialized arrays #47200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\Exception\AccessException;
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand Down Expand Up @@ -96,6 +97,10 @@ private function getPropertyValue($data, $propertyPath)
try {
return $this->propertyAccessor->getValue($data, $propertyPath);
} catch (AccessException $e) {
if (\is_array($data) && $e instanceof NoSuchIndexException) {
return null;
}

if (!$e instanceof UninitializedPropertyException
// For versions without UninitializedPropertyException check the exception message
&& (class_exists(UninitializedPropertyException::class) || !str_contains($e->getMessage(), 'You should initialize it'))
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Exception\AlreadySubmittedException;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler;
Expand All @@ -36,6 +37,7 @@
use Symfony\Component\Form\Tests\Fixtures\Map;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PropertyAccess\PropertyAccess;

class CompoundFormTest extends TestCase
{
Expand Down Expand Up @@ -1076,6 +1078,30 @@ public function testFileUpload()
$this->assertNull($this->form->get('bar')->getData());
}

public function testMapDateTimeObjectsWithEmptyArrayData()
{
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
->enableExceptionOnInvalidIndex()
->getPropertyAccessor();
$form = $this->factory->createBuilder()
->setDataMapper(new PropertyPathMapper($propertyAccessor))
->add('date', DateType::class, [
'auto_initialize' => false,
'format' => 'dd/MM/yyyy',
'html5' => false,
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
])
->getForm();

$form->submit([
'date' => '04/08/2022',
]);

$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
}

private function createForm(string $name = 'name', bool $compound = true): FormInterface
{
$builder = $this->getBuilder($name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigBuilder;
use Symfony\Component\Form\FormFactoryBuilder;
use Symfony\Component\Form\Tests\Fixtures\TypehintedPropertiesCar;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand Down Expand Up @@ -362,6 +364,30 @@ public function provideDate()
[new \DateTimeImmutable()],
];
}

public function testMapFormsToDataMapsDateTimeInstanceToArrayIfNotSetBefore()
{
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
->enableExceptionOnInvalidIndex()
->getPropertyAccessor();
$form = (new FormFactoryBuilder())->getFormFactory()->createBuilder()
->setDataMapper(new PropertyPathMapper($propertyAccessor))
->add('date', DateType::class, [
'auto_initialize' => false,
'format' => 'dd/MM/yyyy',
'html5' => false,
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
])
->getForm();

$form->submit([
'date' => '04/08/2022',
]);

$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
}
}

class SubmittedForm extends Form
Expand Down