Skip to content

[Form] silently ignore uninitialized properties when mapping data to forms #37520

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
Jul 9, 2020
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 @@ -13,6 +13,8 @@

use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\Exception\AccessException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

Expand Down Expand Up @@ -46,7 +48,7 @@ public function mapDataToForms($data, $forms)
$config = $form->getConfig();

if (!$empty && null !== $propertyPath && $config->getMapped()) {
$form->setData($this->propertyAccessor->getValue($data, $propertyPath));
$form->setData($this->getPropertyValue($data, $propertyPath));
} else {
$form->setData($config->getData());
}
Expand Down Expand Up @@ -76,16 +78,32 @@ public function mapFormsToData($forms, &$data)
$propertyValue = $form->getData();
// If the field is of type DateTimeInterface and the data is the same skip the update to
// keep the original object hash
if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->propertyAccessor->getValue($data, $propertyPath)) {
if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->getPropertyValue($data, $propertyPath)) {
continue;
}

// If the data is identical to the value in $data, we are
// dealing with a reference
if (!\is_object($data) || !$config->getByReference() || $propertyValue !== $this->propertyAccessor->getValue($data, $propertyPath)) {
if (!\is_object($data) || !$config->getByReference() || $propertyValue !== $this->getPropertyValue($data, $propertyPath)) {
$this->propertyAccessor->setValue($data, $propertyPath, $propertyValue);
}
}
}
}

private function getPropertyValue($data, $propertyPath)
{
try {
return $this->propertyAccessor->getValue($data, $propertyPath);
} catch (AccessException $e) {
if (!$e instanceof UninitializedPropertyException
// For versions without UninitializedPropertyException check the exception message
&& (class_exists(UninitializedPropertyException::class) || false === strpos($e->getMessage(), 'You should initialize it'))
) {
throw $e;
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigBuilder;
use Symfony\Component\Form\Tests\Fixtures\TypehintedPropertiesCar;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyAccess\PropertyPath;
Expand Down Expand Up @@ -113,6 +114,23 @@ public function testMapDataToFormsIgnoresUnmapped()
$this->assertNull($form->getData());
}

/**
* @requires PHP 7.4
*/
public function testMapDataToFormsIgnoresUninitializedProperties()
{
$engineForm = new Form(new FormConfigBuilder('engine', null, $this->dispatcher));
$colorForm = new Form(new FormConfigBuilder('color', null, $this->dispatcher));

$car = new TypehintedPropertiesCar();
$car->engine = 'BMW';

$this->mapper->mapDataToForms($car, [$engineForm, $colorForm]);

$this->assertSame($car->engine, $engineForm->getData());
$this->assertNull($colorForm->getData());
}

public function testMapDataToFormsSetsDefaultDataIfPassedDataIsNull()
{
$default = new \stdClass();
Expand Down Expand Up @@ -293,13 +311,28 @@ public function testMapFormsToDataIgnoresDisabled()
$config->setPropertyPath($propertyPath);
$config->setData($engine);
$config->setDisabled(true);
$form = new Form($config);
$form = new SubmittedForm($config);

$this->mapper->mapFormsToData([$form], $car);

$this->assertSame($initialEngine, $car->engine);
}

/**
* @requires PHP 7.4
*/
public function testMapFormsToUninitializedProperties()
{
$car = new TypehintedPropertiesCar();
$config = new FormConfigBuilder('engine', null, $this->dispatcher);
$config->setData('BMW');
$form = new SubmittedForm($config);

$this->mapper->mapFormsToData([$form], $car);

$this->assertSame('BMW', $car->engine);
}

/**
* @dataProvider provideDate
*/
Expand Down Expand Up @@ -339,7 +372,7 @@ public function isSubmitted()
}
}

class NotSynchronizedForm extends Form
class NotSynchronizedForm extends SubmittedForm
{
public function isSynchronized()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures;

class TypehintedPropertiesCar
{
public ?string $engine;
public ?string $color;
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy