Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CHANGELOG
* Deprecate the `VersionAwareTest` trait, use feature detection instead
* Add support for the `calendar` option in `DateType`
* Add `LazyChoiceLoader` and `choice_lazy` option in `ChoiceType` for loading and rendering choices on demand
* Use `form.post_set_data` instead of `form.pre_set_data` in `ResizeFormListener`
* Change the priority of `DataCollectorListener` from 255 to -255

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Extension\Core\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Event\PostSetDataEvent;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
Expand All @@ -27,6 +28,9 @@ class ResizeFormListener implements EventSubscriberInterface
protected array $prototypeOptions;

private \Closure|bool $deleteEmpty;
// BC, to be removed in 8.0
private bool $overridden = true;
private bool $usePreSetData = false;

public function __construct(
private string $type,
Expand All @@ -44,15 +48,57 @@ public function __construct(
public static function getSubscribedEvents(): array
{
return [
FormEvents::PRE_SET_DATA => 'preSetData',
FormEvents::PRE_SET_DATA => 'preSetData', // deprecated
FormEvents::POST_SET_DATA => ['postSetData', 255], // as early as possible
Copy link
Member

@stof stof Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it actually allowed to modify the structure of the form on postSetData ? To me, this is too late as those child fields won't be populated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, the form is not cloned and can be modified by reference https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Form/Form.php#L327.
The only event that forbids it is POST_SUBMIT thanks to this check https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Form/Form.php#L260, in such case $this->defaultDataSet is true, but not enough, only data cannot further be changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, this is too late as those child fields won't be populated.

They will be: https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Form/Form.php#L776.

Copy link
Contributor Author

@HeahDude HeahDude Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've refactored the tests to ensure data is mapped consistently: 6588362.

FormEvents::PRE_SUBMIT => 'preSubmit',
// (MergeCollectionListener, MergeDoctrineCollectionListener)
FormEvents::SUBMIT => ['onSubmit', 50],
];
}

/**
* @deprecated Since Symfony 7.2, use {@see postSetData()} instead.
*/
public function preSetData(FormEvent $event): void
{
if (__CLASS__ === static::class
|| __CLASS__ === (new \ReflectionClass($this))->getMethod('preSetData')->getDeclaringClass()->name
) {
// not a child class, or child class does not overload PRE_SET_DATA
return;
}

trigger_deprecation('symfony/form', '7.2', 'Calling "%s()" is deprecated, use "%s::postSetData()" instead.', __METHOD__, __CLASS__);
// parent::preSetData() has been called
$this->overridden = false;
try {
$this->postSetData($event);
} finally {
$this->usePreSetData = true;
}
}

/**
* Remove FormEvent type hint in 8.0.
*
* @final since Symfony 7.2
*/
public function postSetData(FormEvent|PostSetDataEvent $event): void
{
if (__CLASS__ !== static::class) {
if ($this->overridden) {
trigger_deprecation('symfony/form', '7.2', 'Calling "%s::preSetData()" is deprecated, use "%s::postSetData()" instead.', static::class, __CLASS__);
// parent::preSetData() has not been called, noop

return;
}

if ($this->usePreSetData) {
// nothing else to do
return;
}
}

$form = $event->getForm();
$data = $event->getData() ?? [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function __construct(
public static function getSubscribedEvents(): array
{
return [
// High priority in order to be called as soon as possible
FormEvents::POST_SET_DATA => ['postSetData', 255],
// Low priority in order to be called as late as possible
FormEvents::POST_SET_DATA => ['postSetData', -255],
// Low priority in order to be called as late as possible
FormEvents::POST_SUBMIT => ['postSubmit', -255],
];
Expand Down
Loading