Skip to content

Commit 8474340

Browse files
bug #29185 [Form] Fixed keeping hash of equal \DateTimeInterface on submit (HeahDude)
This PR was merged into the 2.8 branch. Discussion ---------- [Form] Fixed keeping hash of equal \DateTimeInterface on submit | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | ~ Commits ------- bc2e2cb5ad [Form] Fixed keeping hash of equal \DateTimeInterface on submit
2 parents 888d9e2 + 108b0ab commit 8474340

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

Extension/Core/DataMapper/PropertyPathMapper.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,17 @@ public function mapFormsToData($forms, &$data)
7373
// Write-back is disabled if the form is not synchronized (transformation failed),
7474
// if the form was not submitted and if the form is disabled (modification not allowed)
7575
if (null !== $propertyPath && $config->getMapped() && $form->isSubmitted() && $form->isSynchronized() && !$form->isDisabled()) {
76-
// If the field is of type DateTime and the data is the same skip the update to
76+
$propertyValue = $form->getData();
77+
// If the field is of type DateTime or DateTimeInterface and the data is the same skip the update to
7778
// keep the original object hash
78-
if ($form->getData() instanceof \DateTime && $form->getData() == $this->propertyAccessor->getValue($data, $propertyPath)) {
79+
if (($propertyValue instanceof \DateTime || $propertyValue instanceof \DateTimeInterface) && $propertyValue == $this->propertyAccessor->getValue($data, $propertyPath)) {
7980
continue;
8081
}
8182

8283
// If the data is identical to the value in $data, we are
8384
// dealing with a reference
84-
if (!\is_object($data) || !$config->getByReference() || $form->getData() !== $this->propertyAccessor->getValue($data, $propertyPath)) {
85-
$this->propertyAccessor->setValue($data, $propertyPath, $form->getData());
85+
if (!\is_object($data) || !$config->getByReference() || $propertyValue !== $this->propertyAccessor->getValue($data, $propertyPath)) {
86+
$this->propertyAccessor->setValue($data, $propertyPath, $propertyValue);
8687
}
8788
}
8889
}

Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,44 @@ public function testMapFormsToDataIgnoresDisabled()
357357

358358
$this->mapper->mapFormsToData(array($form), $car);
359359
}
360+
361+
/**
362+
* @dataProvider provideDate
363+
*/
364+
public function testMapFormsToDataDoesNotChangeEqualDateTimeInstance($date)
365+
{
366+
$article = array();
367+
$publishedAt = $date;
368+
$article['publishedAt'] = clone $publishedAt;
369+
$propertyPath = $this->getPropertyPath('[publishedAt]');
370+
371+
$this->propertyAccessor->expects($this->once())
372+
->method('getValue')
373+
->willReturn($article['publishedAt'])
374+
;
375+
$this->propertyAccessor->expects($this->never())
376+
->method('setValue')
377+
;
378+
379+
$config = new FormConfigBuilder('publishedAt', \get_class($publishedAt), $this->dispatcher);
380+
$config->setByReference(false);
381+
$config->setPropertyPath($propertyPath);
382+
$config->setData($publishedAt);
383+
$form = $this->getForm($config);
384+
385+
$this->mapper->mapFormsToData(array($form), $article);
386+
}
387+
388+
public function provideDate()
389+
{
390+
$data = array(
391+
'\DateTime' => array(new \DateTime()),
392+
);
393+
394+
if (class_exists('DateTimeImmutable')) {
395+
$data['\DateTimeImmutable'] = array(new \DateTimeImmutable());
396+
}
397+
398+
return $data;
399+
}
360400
}

0 commit comments

Comments
 (0)