From bc2e2cb5ad8a347cbab6162ce9157b0af02ec426 Mon Sep 17 00:00:00 2001 From: Jules Pietri Date: Mon, 12 Nov 2018 11:48:30 +0100 Subject: [PATCH] [Form] Fixed keeping hash of equal \DateTimeInterface on submit --- .../Core/DataMapper/PropertyPathMapper.php | 9 +++-- .../DataMapper/PropertyPathMapperTest.php | 40 +++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php b/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php index f9721e52b176..e8dbabdb9fb9 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php +++ b/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php @@ -73,16 +73,17 @@ public function mapFormsToData($forms, &$data) // Write-back is disabled if the form is not synchronized (transformation failed), // if the form was not submitted and if the form is disabled (modification not allowed) if (null !== $propertyPath && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) { - // If the field is of type DateTime and the data is the same skip the update to + $propertyValue = $form->getData(); + // If the field is of type DateTime or DateTimeInterface and the data is the same skip the update to // keep the original object hash - if ($form->getData() instanceof \DateTime && $form->getData() == $this->propertyAccessor->getValue($data, $propertyPath)) { + if (($propertyValue instanceof \DateTime || $propertyValue instanceof \DateTimeInterface) && $propertyValue == $this->propertyAccessor->getValue($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() || $form->getData() !== $this->propertyAccessor->getValue($data, $propertyPath)) { - $this->propertyAccessor->setValue($data, $propertyPath, $form->getData()); + if (!\is_object($data) || !$config->getByReference() || $propertyValue !== $this->propertyAccessor->getValue($data, $propertyPath)) { + $this->propertyAccessor->setValue($data, $propertyPath, $propertyValue); } } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php index 979adcdceedd..1dcff2f4020a 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php @@ -357,4 +357,44 @@ public function testMapFormsToDataIgnoresDisabled() $this->mapper->mapFormsToData(array($form), $car); } + + /** + * @dataProvider provideDate + */ + public function testMapFormsToDataDoesNotChangeEqualDateTimeInstance($date) + { + $article = array(); + $publishedAt = $date; + $article['publishedAt'] = clone $publishedAt; + $propertyPath = $this->getPropertyPath('[publishedAt]'); + + $this->propertyAccessor->expects($this->once()) + ->method('getValue') + ->willReturn($article['publishedAt']) + ; + $this->propertyAccessor->expects($this->never()) + ->method('setValue') + ; + + $config = new FormConfigBuilder('publishedAt', \get_class($publishedAt), $this->dispatcher); + $config->setByReference(false); + $config->setPropertyPath($propertyPath); + $config->setData($publishedAt); + $form = $this->getForm($config); + + $this->mapper->mapFormsToData(array($form), $article); + } + + public function provideDate() + { + $data = array( + '\DateTime' => array(new \DateTime()), + ); + + if (class_exists('DateTimeImmutable')) { + $data['\DateTimeImmutable'] = array(new \DateTimeImmutable()); + } + + return $data; + } }