Skip to content

Commit

Permalink
minor #50128 46867 - add more helpful property path accessor exceptio…
Browse files Browse the repository at this point in the history
…ns (patrickmaynard)

This PR was squashed before being merged into the 7.1 branch.

Discussion
----------

46867 - add more helpful property path accessor exceptions

| Q             | A
| ------------- | ---
| Branch? | 6.3
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Tickets | symfony/symfony#46867
| License | MIT
| Doc PR | none

Dear reviewers,

This small modification adds more helpful exceptions when properties cannot be accessed/set
using forms. Only one file was modified, so it should be relatively easy to review. Please feel free to leave a comment if you have any questions about what I'm doing here, and thanks for your work as a reviewer on an open-source project!

All the best,

Patrick

Commits
-------

8d87a672a2 46867 - add more helpful property path accessor exceptions
  • Loading branch information
fabpot committed Mar 17, 2024
2 parents 3d545be + fd6e0a5 commit a8f6af4
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Extension/Core/DataAccessor/PropertyPathAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\Exception\AccessException as PropertyAccessException;
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand Down Expand Up @@ -60,7 +61,11 @@ public function setValue(object|array &$data, mixed $value, FormInterface $form)
// 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)) {
$this->propertyAccessor->setValue($data, $propertyPath, $value);
try {
$this->propertyAccessor->setValue($data, $propertyPath, $value);
} catch (NoSuchPropertyException $e) {
throw new NoSuchPropertyException($e->getMessage().' Make the property public, add a setter, or set the "mapped" field option in the form type to be false.', 0, $e);
}
}
}

Expand Down

0 comments on commit a8f6af4

Please sign in to comment.