Skip to content

Commit 896c5f2

Browse files
HeahDudexabbuh
authored andcommitted
[Form] Use form.post_set_data in ResizeFormListener
1 parent e3fe767 commit 896c5f2

File tree

4 files changed

+233
-114
lines changed

4 files changed

+233
-114
lines changed

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
---

Extension/Core/EventListener/ResizeFormListener.php

+47-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,57 @@ 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+
* Remove FormEvent type hint in 8.0.
83+
*
84+
* @final since Symfony 7.2
85+
*/
86+
public function postSetData(FormEvent|PostSetDataEvent $event): void
87+
{
88+
if (__CLASS__ !== static::class) {
89+
if ($this->overridden) {
90+
trigger_deprecation('symfony/form', '7.2', 'Calling "%s::preSetData()" is deprecated, use "%s::postSetData()" instead.', static::class, __CLASS__);
91+
// parent::preSetData() has not been called, noop
92+
93+
return;
94+
}
95+
96+
if ($this->usePreSetData) {
97+
// nothing else to do
98+
return;
99+
}
100+
}
101+
56102
$form = $event->getForm();
57103
$data = $event->getData() ?? [];
58104

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)