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

[Form] read form values using the chain data accessor #54723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace Symfony\Component\Form\Extension\Core\DataAccessor;

use Symfony\Component\Form\DataAccessorInterface;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\AccessException;
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\Exception\AccessException as PropertyAccessException;
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
Expand Down Expand Up @@ -51,21 +53,31 @@
/**
* {@inheritdoc}
*/
public function setValue(&$data, $propertyValue, FormInterface $form): void

Check failure on line 56 in src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php:56:38: ParamNameMismatch: Argument 2 of Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor::setValue has wrong name $propertyValue, expecting $value as defined by Symfony\Component\Form\DataAccessorInterface::setValue (see https://psalm.dev/230)

Check failure on line 56 in src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php:56:38: ParamNameMismatch: Argument 2 of Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor::setValue has wrong name $propertyValue, expecting $value as defined by Symfony\Component\Form\DataAccessorInterface::setValue (see https://psalm.dev/230)
{
if (null === $propertyPath = $form->getPropertyPath()) {
throw new AccessException('Unable to write the given value as no property path is defined.');
}

$getValue = function () use ($data, $form, $propertyPath) {
$dataMapper = $this->getDataMapper($form);

if ($dataMapper instanceof DataMapper && null !== $dataAccessor = $dataMapper->getDataAccessor()) {
return $dataAccessor->getValue($data, $form);
}

return $this->getPropertyValue($data, $propertyPath);
};

// If the field is of type DateTimeInterface and the data is the same skip the update to
// keep the original object hash
if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $this->getPropertyValue($data, $propertyPath)) {
if ($propertyValue instanceof \DateTimeInterface && $propertyValue == $getValue()) {
return;
}

// If the data is identical to the value in $data, we are
// dealing with a reference
if (!\is_object($data) || !$form->getConfig()->getByReference() || $propertyValue !== $this->getPropertyValue($data, $propertyPath)) {
if (!\is_object($data) || !$form->getConfig()->getByReference() || $propertyValue !== $getValue()) {
$this->propertyAccessor->setValue($data, $propertyPath, $propertyValue);
}
}
Expand All @@ -81,7 +93,7 @@
/**
* {@inheritdoc}
*/
public function isWritable($data, FormInterface $form): bool

Check failure on line 96 in src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php:96:32: ParamNameMismatch: Argument 1 of Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor::isWritable has wrong name $data, expecting $viewData as defined by Symfony\Component\Form\DataAccessorInterface::isWritable (see https://psalm.dev/230)

Check failure on line 96 in src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php

View workflow job for this annotation

GitHub Actions / Psalm

ParamNameMismatch

src/Symfony/Component/Form/Extension/Core/DataAccessor/PropertyPathAccessor.php:96:32: ParamNameMismatch: Argument 1 of Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor::isWritable has wrong name $data, expecting $viewData as defined by Symfony\Component\Form\DataAccessorInterface::isWritable (see https://psalm.dev/230)
{
return null !== $form->getPropertyPath();
}
Expand All @@ -105,4 +117,13 @@
return null;
}
}

private function getDataMapper(FormInterface $form): ?DataMapperInterface
{
do {
$dataMapper = $form->getConfig()->getDataMapper();
} while (null === $dataMapper && null !== $form = $form->getParent());

return $dataMapper;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,12 @@ public function mapFormsToData(iterable $forms, &$data): void
}
}
}

/**
* @internal
*/
public function getDataAccessor(): DataAccessorInterface
{
return $this->dataAccessor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigBuilder;
use Symfony\Component\Form\FormFactoryBuilder;
Expand Down Expand Up @@ -419,6 +421,25 @@ public function testMapFormsToDataMapsDateTimeInstanceToArrayIfNotSetBefore()

$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
}

public function testMapFormToDataWithOnlyGetterConfigured()
{
$person = new DummyPerson('foo');
$form = (new FormFactoryBuilder())
->getFormFactory()
->createBuilder(FormType::class, $person)
->add('name', TextType::class, [
'getter' => function (DummyPerson $person) {
return $person->myName();
},
])
->getForm();
$form->submit([
'name' => 'bar',
]);

$this->assertSame('bar', $person->myName());
}
}

class SubmittedForm extends Form
Expand Down Expand Up @@ -455,4 +476,9 @@ public function rename($name): void
{
$this->name = $name;
}

public function setName($name): void
{
$this->name = $name;
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.