Skip to content

Commit 706fe13

Browse files
committed
Merge branch '7.0' into 7.1
* 7.0: (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 ba441cc + b4df6a3 commit 706fe13

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;
@@ -52,15 +54,25 @@ public function setValue(object|array &$data, mixed $value, FormInterface $form)
5254
throw new AccessException('Unable to write the given value as no property path is defined.');
5355
}
5456

57+
$getValue = function () use ($data, $form, $propertyPath) {
58+
$dataMapper = $this->getDataMapper($form);
59+
60+
if ($dataMapper instanceof DataMapper && null !== $dataAccessor = $dataMapper->getDataAccessor()) {
61+
return $dataAccessor->getValue($data, $form);
62+
}
63+
64+
return $this->getPropertyValue($data, $propertyPath);
65+
};
66+
5567
// If the field is of type DateTimeInterface and the data is the same skip the update to
5668
// keep the original object hash
57-
if ($value instanceof \DateTimeInterface && $value == $this->getPropertyValue($data, $propertyPath)) {
69+
if ($value instanceof \DateTimeInterface && $value == $getValue()) {
5870
return;
5971
}
6072

6173
// If the data is identical to the value in $data, we are
6274
// dealing with a reference
63-
if (!\is_object($data) || !$form->getConfig()->getByReference() || $value !== $this->getPropertyValue($data, $propertyPath)) {
75+
if (!\is_object($data) || !$form->getConfig()->getByReference() || $value !== $getValue()) {
6476
try {
6577
$this->propertyAccessor->setValue($data, $propertyPath, $value);
6678
} catch (NoSuchPropertyException $e) {
@@ -98,4 +110,13 @@ private function getPropertyValue(object|array $data, PropertyPathInterface $pro
98110
return null;
99111
}
100112
}
113+
114+
private function getDataMapper(FormInterface $form): ?DataMapperInterface
115+
{
116+
do {
117+
$dataMapper = $form->getConfig()->getDataMapper();
118+
} while (null === $dataMapper && null !== $form = $form->getParent());
119+
120+
return $dataMapper;
121+
}
101122
}

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)