Skip to content

Commit

Permalink
bug #29185 [Form] Fixed keeping hash of equal \DateTimeInterface on s…
Browse files Browse the repository at this point in the history
…ubmit (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
-------

bc2e2cb [Form] Fixed keeping hash of equal \DateTimeInterface on submit
  • Loading branch information
nicolas-grekas committed Nov 13, 2018
2 parents 19b0189 + bc2e2cb commit 32c0172
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 32c0172

Please sign in to comment.