Skip to content

Commit 768c6b4

Browse files
HeahDudexabbuh
authored andcommitted
[Form] Use form.post_set_data in ResizeFormListener
1 parent d83167d commit 768c6b4

File tree

4 files changed

+234
-114
lines changed

4 files changed

+234
-114
lines changed

src/Symfony/Component/Form/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ CHANGELOG
77
* Deprecate the `VersionAwareTest` trait, use feature detection instead
88
* Add support for the `calendar` option in `DateType`
99
* Add `LazyChoiceLoader` and `choice_lazy` option in `ChoiceType` for loading and rendering choices on demand
10+
* Use `form.post_set_data` instead of `form.pre_set_data` in `ResizeFormListener`
11+
* Change the priority of `DataCollectorListener` from 255 to -255
1012

1113
7.1
1214
---

src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php

+48-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Form\Extension\Core\EventListener;
1313

1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\Form\Event\PostSetDataEvent;
1516
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1617
use Symfony\Component\Form\FormEvent;
1718
use Symfony\Component\Form\FormEvents;
@@ -27,6 +28,9 @@ class ResizeFormListener implements EventSubscriberInterface
2728
protected array $prototypeOptions;
2829

2930
private \Closure|bool $deleteEmpty;
31+
// BC, to be removed in 8.0
32+
private bool $overridden = true;
33+
private bool $usePreSetData = false;
3034

3135
public function __construct(
3236
private string $type,
@@ -44,15 +48,58 @@ public function __construct(
4448
public static function getSubscribedEvents(): array
4549
{
4650
return [
47-
FormEvents::PRE_SET_DATA => 'preSetData',
51+
FormEvents::PRE_SET_DATA => 'preSetData', // deprecated
52+
FormEvents::POST_SET_DATA => ['postSetData', 255], // as early as possible
4853
FormEvents::PRE_SUBMIT => 'preSubmit',
4954
// (MergeCollectionListener, MergeDoctrineCollectionListener)
5055
FormEvents::SUBMIT => ['onSubmit', 50],
5156
];
5257
}
5358

59+
/**
60+
* @deprecated Since Symfony 7.2, use {@see postSetData()} instead.
61+
*/
5462
public function preSetData(FormEvent $event): void
5563
{
64+
if (__CLASS__ === static::class
65+
|| __CLASS__ === (new \ReflectionClass($this))->getMethod('preSetData')->getDeclaringClass()->name
66+
) {
67+
// not a child class, or child class does not overload PRE_SET_DATA
68+
return;
69+
}
70+
71+
trigger_deprecation('symfony/form', '7.2', 'Calling "%s()" is deprecated, use "%s::postSetData()" instead.', __METHOD__, __CLASS__);
72+
// parent::preSetData() has been called
73+
$this->overridden = false;
74+
try {
75+
$this->postSetData($event);
76+
} finally {
77+
$this->usePreSetData = true;
78+
}
79+
}
80+
81+
82+
/**
83+
* Remove FormEvent type hint in 8.0.
84+
*
85+
* @final since Symfony 7.2
86+
*/
87+
public function postSetData(FormEvent|PostSetDataEvent $event): void
88+
{
89+
if (__CLASS__ !== static::class) {
90+
if ($this->overridden) {
91+
trigger_deprecation('symfony/form', '7.2', 'Calling "%s::preSetData()" is deprecated, use "%s::postSetData()" instead.', static::class, __CLASS__);
92+
// parent::preSetData() has not been called, noop
93+
94+
return;
95+
}
96+
97+
if ($this->usePreSetData) {
98+
// nothing else to do
99+
return;
100+
}
101+
}
102+
56103
$form = $event->getForm();
57104
$data = $event->getData() ?? [];
58105

src/Symfony/Component/Form/Extension/DataCollector/EventListener/DataCollectorListener.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function __construct(
3232
public static function getSubscribedEvents(): array
3333
{
3434
return [
35-
// High priority in order to be called as soon as possible
36-
FormEvents::POST_SET_DATA => ['postSetData', 255],
35+
// Low priority in order to be called as late as possible
36+
FormEvents::POST_SET_DATA => ['postSetData', -255],
3737
// Low priority in order to be called as late as possible
3838
FormEvents::POST_SUBMIT => ['postSubmit', -255],
3939
];

0 commit comments

Comments
 (0)