Skip to content

Commit

Permalink
[Form] Fixed keeping hash of equal \DateTimeInterface on submit
Browse files Browse the repository at this point in the history
  • Loading branch information
HeahDude committed Nov 12, 2018
1 parent 46e3745 commit bc2e2cb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Expand Up @@ -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);
}
}
}
Expand Down
Expand Up @@ -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;
}
}

0 comments on commit bc2e2cb

Please sign in to comment.