|
12 | 12 | namespace Symfony\Component\Form\Extension\Core\DataAccessor; |
13 | 13 |
|
14 | 14 | use Symfony\Component\Form\DataAccessorInterface; |
| 15 | +use Symfony\Component\Form\DataMapperInterface; |
15 | 16 | use Symfony\Component\Form\Exception\AccessException; |
| 17 | +use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper; |
16 | 18 | use Symfony\Component\Form\FormInterface; |
17 | 19 | use Symfony\Component\PropertyAccess\Exception\AccessException as PropertyAccessException; |
18 | 20 | use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException; |
@@ -57,15 +59,25 @@ public function setValue(&$data, $propertyValue, FormInterface $form): void |
57 | 59 | throw new AccessException('Unable to write the given value as no property path is defined.'); |
58 | 60 | } |
59 | 61 |
|
| 62 | + $getValue = function () use ($data, $form, $propertyPath) { |
| 63 | + $dataMapper = $this->getDataMapper($form); |
| 64 | + |
| 65 | + if ($dataMapper instanceof DataMapper && null !== $dataAccessor = $dataMapper->getDataAccessor()) { |
| 66 | + return $dataAccessor->getValue($data, $form); |
| 67 | + } |
| 68 | + |
| 69 | + return $this->getPropertyValue($data, $propertyPath); |
| 70 | + }; |
| 71 | + |
60 | 72 | // If the field is of type DateTimeInterface and the data is the same skip the update to |
61 | 73 | // keep the original object hash |
62 | | - if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->getPropertyValue($data, $propertyPath)) { |
| 74 | + if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $getValue()) { |
63 | 75 | return; |
64 | 76 | } |
65 | 77 |
|
66 | 78 | // If the data is identical to the value in $data, we are |
67 | 79 | // dealing with a reference |
68 | | - if (!\is_object($data) || !$form->getConfig()->getByReference() || $propertyValue !== $this->getPropertyValue($data, $propertyPath)) { |
| 80 | + if (!\is_object($data) || !$form->getConfig()->getByReference() || $propertyValue !== $getValue()) { |
69 | 81 | $this->propertyAccessor->setValue($data, $propertyPath, $propertyValue); |
70 | 82 | } |
71 | 83 | } |
@@ -105,4 +117,13 @@ private function getPropertyValue($data, PropertyPathInterface $propertyPath) |
105 | 117 | return null; |
106 | 118 | } |
107 | 119 | } |
| 120 | + |
| 121 | + private function getDataMapper(FormInterface $form): ?DataMapperInterface |
| 122 | + { |
| 123 | + do { |
| 124 | + $dataMapper = $form->getConfig()->getDataMapper(); |
| 125 | + } while (null === $dataMapper && null !== $form = $form->getParent()); |
| 126 | + |
| 127 | + return $dataMapper; |
| 128 | + } |
108 | 129 | } |
0 commit comments