[Form] Fixed keeping hash of equal \DateTimeInterface on submit
This commit is contained in:
parent
46e3745a03
commit
bc2e2cb5ad
@ -73,16 +73,17 @@ class PropertyPathMapper implements DataMapperInterface
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -357,4 +357,44 @@ class PropertyPathMapperTest extends TestCase
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user