|
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;
|
@@ -51,15 +53,25 @@ public function setValue(object|array &$data, mixed $value, FormInterface $form)
|
51 | 53 | throw new AccessException('Unable to write the given value as no property path is defined.');
|
52 | 54 | }
|
53 | 55 |
|
| 56 | + $getValue = function () use ($data, $form, $propertyPath) { |
| 57 | + $dataMapper = $this->getDataMapper($form); |
| 58 | + |
| 59 | + if ($dataMapper instanceof DataMapper && null !== $dataAccessor = $dataMapper->getDataAccessor()) { |
| 60 | + return $dataAccessor->getValue($data, $form); |
| 61 | + } |
| 62 | + |
| 63 | + return $this->getPropertyValue($data, $propertyPath); |
| 64 | + }; |
| 65 | + |
54 | 66 | // If the field is of type DateTimeInterface and the data is the same skip the update to
|
55 | 67 | // keep the original object hash
|
56 |
| - if ($value instanceof \DateTimeInterface && $value == $this->getPropertyValue($data, $propertyPath)) { |
| 68 | + if ($value instanceof \DateTimeInterface && $value == $getValue()) { |
57 | 69 | return;
|
58 | 70 | }
|
59 | 71 |
|
60 | 72 | // If the data is identical to the value in $data, we are
|
61 | 73 | // dealing with a reference
|
62 |
| - if (!\is_object($data) || !$form->getConfig()->getByReference() || $value !== $this->getPropertyValue($data, $propertyPath)) { |
| 74 | + if (!\is_object($data) || !$form->getConfig()->getByReference() || $value !== $getValue()) { |
63 | 75 | $this->propertyAccessor->setValue($data, $propertyPath, $value);
|
64 | 76 | }
|
65 | 77 | }
|
@@ -93,4 +105,13 @@ private function getPropertyValue(object|array $data, PropertyPathInterface $pro
|
93 | 105 | return null;
|
94 | 106 | }
|
95 | 107 | }
|
| 108 | + |
| 109 | + private function getDataMapper(FormInterface $form): ?DataMapperInterface |
| 110 | + { |
| 111 | + do { |
| 112 | + $dataMapper = $form->getConfig()->getDataMapper(); |
| 113 | + } while (null === $dataMapper && null !== $form = $form->getParent()); |
| 114 | + |
| 115 | + return $dataMapper; |
| 116 | + } |
96 | 117 | }
|
0 commit comments