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

[Form] ignore missing keys when mapping DateTime objects to uninitialized arrrays #47203

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 9, 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 @@ -15,6 +15,7 @@
use Symfony\Component\Form\Exception\AccessException;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\Exception\AccessException as PropertyAccessException;
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 @@ -90,6 +91,10 @@ private function getPropertyValue($data, PropertyPathInterface $propertyPath)
try {
return $this->propertyAccessor->getValue($data, $propertyPath);
} catch (PropertyAccessException $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) || false === strpos($e->getMessage(), 'You should initialize it'))
Expand Down
30 changes: 29 additions & 1 deletion 30 src/Symfony/Component/Form/Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Exception\AlreadySubmittedException;
use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Extension\Core\Type\DateType;
Expand Down Expand Up @@ -1079,7 +1080,10 @@ public function testFileUpload()
$this->assertNull($this->form->get('bar')->getData());
}

public function testMapDateTimeObjectsWithEmptyArrayData()
/**
* @group legacy
*/
public function testMapDateTimeObjectsWithEmptyArrayDataUsingPropertyPathMapper()
{
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
->enableExceptionOnInvalidIndex()
Expand All @@ -1103,6 +1107,30 @@ public function testMapDateTimeObjectsWithEmptyArrayData()
$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
}

public function testMapDateTimeObjectsWithEmptyArrayDataUsingDataMapper()
{
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
->enableExceptionOnInvalidIndex()
->getPropertyAccessor();
$form = $this->factory->createBuilder()
->setDataMapper(new DataMapper(new PropertyPathAccessor($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 @@ -14,10 +14,14 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
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\PropertyPath;

class DataMapperTest extends TestCase
Expand Down Expand Up @@ -388,6 +392,33 @@ public function testMapFormsToDataUsingSetCallbackOption()

self::assertSame('Jane Doe', $person->myName());
}

public function testMapFormsToDataMapsDateTimeInstanceToArrayIfNotSetBefore()
{
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
->enableExceptionOnInvalidIndex()
->getPropertyAccessor();
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
->enableExceptionOnInvalidIndex()
->getPropertyAccessor();
$form = (new FormFactoryBuilder())->getFormFactory()->createBuilder()
->setDataMapper(new DataMapper(new PropertyPathAccessor($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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.