Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b3fa8e9

Browse filesBrowse files
committed
read form values using the chain data accessor
1 parent 7e5d7dc commit b3fa8e9
Copy full SHA for b3fa8e9

File tree

3 files changed

+49
-5
lines changed
Filter options

3 files changed

+49
-5
lines changed

‎src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php
+17-2Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
class PropertyPathAccessor implements DataAccessorInterface
3131
{
3232
private $propertyAccessor;
33+
/** @var DataAccessorInterface|null */
34+
private $readAccessor;
3335

3436
public function __construct(?PropertyAccessorInterface $propertyAccessor = null)
3537
{
@@ -57,15 +59,23 @@ public function setValue(&$data, $propertyValue, FormInterface $form): void
5759
throw new AccessException('Unable to write the given value as no property path is defined.');
5860
}
5961

62+
$getValue = function () use ($data, $form, $propertyPath) {
63+
if (null !== $this->readAccessor) {
64+
return $this->readAccessor->getValue($data, $form);
65+
}
66+
67+
return $this->getPropertyValue($data, $propertyPath);
68+
};
69+
6070
// If the field is of type DateTimeInterface and the data is the same skip the update to
6171
// keep the original object hash
62-
if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->getPropertyValue($data, $propertyPath)) {
72+
if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $getValue()) {
6373
return;
6474
}
6575

6676
// If the data is identical to the value in $data, we are
6777
// dealing with a reference
68-
if (!\is_object($data) || !$form->getConfig()->getByReference() || $propertyValue !== $this->getPropertyValue($data, $propertyPath)) {
78+
if (!\is_object($data) || !$form->getConfig()->getByReference() || $propertyValue !== $getValue()) {
6979
$this->propertyAccessor->setValue($data, $propertyPath, $propertyValue);
7080
}
7181
}
@@ -86,6 +96,11 @@ public function isWritable($data, FormInterface $form): bool
8696
return null !== $form->getPropertyPath();
8797
}
8898

99+
public function setReadAccessor(DataAccessorInterface $dataAccessor): void
100+
{
101+
$this->readAccessor = $dataAccessor;
102+
}
103+
89104
private function getPropertyValue($data, PropertyPathInterface $propertyPath)
90105
{
91106
try {

‎src/Symfony/Component/Form/Extension/Core/Type/FormType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/FormType.php
+6-3Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ class FormType extends BaseType
3333

3434
public function __construct(?PropertyAccessorInterface $propertyAccessor = null)
3535
{
36-
$this->dataMapper = new DataMapper(new ChainAccessor([
36+
$propertyPathAccessor = new PropertyPathAccessor($propertyAccessor ?? PropertyAccess::createPropertyAccessor());
37+
$dataAccessor = new ChainAccessor([
3738
new CallbackAccessor(),
38-
new PropertyPathAccessor($propertyAccessor ?? PropertyAccess::createPropertyAccessor()),
39-
]));
39+
$propertyPathAccessor,
40+
]);
41+
$propertyPathAccessor->setReadAccessor($dataAccessor);
42+
$this->dataMapper = new DataMapper($dataAccessor);
4043
}
4144

4245
/**

‎src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/DataMapperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/DataMapperTest.php
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
1818
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
1919
use Symfony\Component\Form\Extension\Core\Type\DateType;
20+
use Symfony\Component\Form\Extension\Core\Type\FormType;
21+
use Symfony\Component\Form\Extension\Core\Type\TextType;
2022
use Symfony\Component\Form\Form;
2123
use Symfony\Component\Form\FormConfigBuilder;
2224
use Symfony\Component\Form\FormFactoryBuilder;
@@ -419,6 +421,25 @@ public function testMapFormsToDataMapsDateTimeInstanceToArrayIfNotSetBefore()
419421

420422
$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
421423
}
424+
425+
public function testMapFormToDataWithOnlyGetterConfigured()
426+
{
427+
$person = new DummyPerson('foo');
428+
$form = (new FormFactoryBuilder())
429+
->getFormFactory()
430+
->createBuilder(FormType::class, $person)
431+
->add('name',TextType::class, [
432+
'getter' => function (DummyPerson $person) {
433+
return $person->myName();
434+
},
435+
])
436+
->getForm();
437+
$form->submit([
438+
'name' => 'bar',
439+
]);
440+
441+
$this->assertSame('bar', $person->myName());
442+
}
422443
}
423444

424445
class SubmittedForm extends Form
@@ -455,4 +476,9 @@ public function rename($name): void
455476
{
456477
$this->name = $name;
457478
}
479+
480+
public function setName($name): void
481+
{
482+
$this->name = $name;
483+
}
458484
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.