Skip to content

Commit b4df6a3

Browse files
committed
Merge branch '6.4' into 7.0
* 6.4: (22 commits) fix merge [AssetMapper] Check asset/vendor directory is writable detect wrong e-mail validation modes detect wrong usages of minMessage/maxMessage in options [Security] Remove workflow from empty folder read form values using the chain data accessor [Validator] Review Bulgarian (bg) translation Reviewed italian translation Fix french translation call substr() with integer offsets [Finder] Also consider .git inside the basedir of in() directory review: FR translation [Serializer] Add AbstractNormalizerContextBuilder::defaultConstructorArguments() Update spanish and catalan translations Update AbstractSchemaListener.php to adjust more database params Updated id=113 Arabic translation. #53771 Updated validator Lithuanian translations review: translation RU [PropertyInfo] Fix PHPStan properties type in trait explicitly cast boolean SSL stream options ...
2 parents abf744c + 718f103 commit b4df6a3

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

Extension/Core/DataAccessor/PropertyPathAccessor.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespace Symfony\Component\Form\Extension\Core\DataAccessor;
1313

1414
use Symfony\Component\Form\DataAccessorInterface;
15+
use Symfony\Component\Form\DataMapperInterface;
1516
use Symfony\Component\Form\Exception\AccessException;
17+
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
1618
use Symfony\Component\Form\FormInterface;
1719
use Symfony\Component\PropertyAccess\Exception\AccessException as PropertyAccessException;
1820
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
@@ -51,15 +53,25 @@ public function setValue(object|array &$data, mixed $value, FormInterface $form)
5153
throw new AccessException('Unable to write the given value as no property path is defined.');
5254
}
5355

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+
5466
// If the field is of type DateTimeInterface and the data is the same skip the update to
5567
// keep the original object hash
56-
if ($value instanceof \DateTimeInterface && $value == $this->getPropertyValue($data, $propertyPath)) {
68+
if ($value instanceof \DateTimeInterface && $value == $getValue()) {
5769
return;
5870
}
5971

6072
// If the data is identical to the value in $data, we are
6173
// 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()) {
6375
$this->propertyAccessor->setValue($data, $propertyPath, $value);
6476
}
6577
}
@@ -93,4 +105,13 @@ private function getPropertyValue(object|array $data, PropertyPathInterface $pro
93105
return null;
94106
}
95107
}
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+
}
96117
}

Extension/Core/DataMapper/DataMapper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,12 @@ public function mapFormsToData(\Traversable $forms, mixed &$data): void
7474
}
7575
}
7676
}
77+
78+
/**
79+
* @internal
80+
*/
81+
public function getDataAccessor(): DataAccessorInterface
82+
{
83+
return $this->dataAccessor;
84+
}
7785
}

Tests/Extension/Core/DataMapper/DataMapperTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
1717
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
1818
use Symfony\Component\Form\Extension\Core\Type\DateType;
19+
use Symfony\Component\Form\Extension\Core\Type\FormType;
20+
use Symfony\Component\Form\Extension\Core\Type\TextType;
1921
use Symfony\Component\Form\Form;
2022
use Symfony\Component\Form\FormConfigBuilder;
2123
use Symfony\Component\Form\FormFactoryBuilder;
@@ -403,6 +405,25 @@ public function testMapFormsToDataMapsDateTimeInstanceToArrayIfNotSetBefore()
403405

404406
$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
405407
}
408+
409+
public function testMapFormToDataWithOnlyGetterConfigured()
410+
{
411+
$person = new DummyPerson('foo');
412+
$form = (new FormFactoryBuilder())
413+
->getFormFactory()
414+
->createBuilder(FormType::class, $person)
415+
->add('name', TextType::class, [
416+
'getter' => function (DummyPerson $person) {
417+
return $person->myName();
418+
},
419+
])
420+
->getForm();
421+
$form->submit([
422+
'name' => 'bar',
423+
]);
424+
425+
$this->assertSame('bar', $person->myName());
426+
}
406427
}
407428

408429
class SubmittedForm extends Form
@@ -439,4 +460,9 @@ public function rename($name): void
439460
{
440461
$this->name = $name;
441462
}
463+
464+
public function setName($name): void
465+
{
466+
$this->name = $name;
467+
}
442468
}

0 commit comments

Comments
 (0)