Skip to content

Commit

Permalink
Merge branch '7.0' into 7.1
Browse files Browse the repository at this point in the history
* 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
  ...
  • Loading branch information
xabbuh committed Apr 28, 2024
2 parents ba441cc + b4df6a3 commit 706fe13
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
25 changes: 23 additions & 2 deletions Extension/Core/DataAccessor/PropertyPathAccessor.php
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 @@ -52,15 +54,25 @@ public function setValue(object|array &$data, mixed $value, FormInterface $form)
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 ($value instanceof \DateTimeInterface && $value == $this->getPropertyValue($data, $propertyPath)) {
if ($value instanceof \DateTimeInterface && $value == $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() || $value !== $this->getPropertyValue($data, $propertyPath)) {
if (!\is_object($data) || !$form->getConfig()->getByReference() || $value !== $getValue()) {
try {
$this->propertyAccessor->setValue($data, $propertyPath, $value);
} catch (NoSuchPropertyException $e) {
Expand Down Expand Up @@ -98,4 +110,13 @@ private function getPropertyValue(object|array $data, PropertyPathInterface $pro
return null;
}
}

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

return $dataMapper;
}
}
8 changes: 8 additions & 0 deletions Extension/Core/DataMapper/DataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,12 @@ public function mapFormsToData(\Traversable $forms, mixed &$data): void
}
}
}

/**
* @internal
*/
public function getDataAccessor(): DataAccessorInterface
{
return $this->dataAccessor;
}
}
26 changes: 26 additions & 0 deletions Tests/Extension/Core/DataMapper/DataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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 @@ -403,6 +405,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 @@ -439,4 +460,9 @@ public function rename($name): void
{
$this->name = $name;
}

public function setName($name): void
{
$this->name = $name;
}
}

0 comments on commit 706fe13

Please sign in to comment.