8000 [Form] Use `form.post_set_data` in `ResizeFormListener` · symfony/symfony@ca3acd9 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ca3acd9

Browse files
HeahDudexabbuh
authored andcommitted
[Form] Use form.post_set_data in ResizeFormListener
1 parent cc11de0 commit ca3acd9

File tree

4 files changed

+226
-114
lines changed

4 files changed

+226
-114
lines changed

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* Deprecate the `VersionAwareTest` trait, use feature detection instead
88
* Add support for the `calendar` option in `DateType`
9+
* Use `form.post_set_data` instead of `form.pre_set_data` in `ResizeFormListener`
10+
* Change the priority of `DataCollectorListener` from 255 to -255
911

1012
7.1
1113
---

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

Lines changed: 40 additions & 1 deletion
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,50 @@ 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+
$this->postSetData($event);
75+
$this->usePreSetData = true;
76+
}
77+
78+
// Remove FormEvent type hint in 8.0
79+
final public function postSetData(FormEvent|PostSetDataEvent $event): void
80+
{
81+
if (__CLASS__ !== static::class) {
82+
if ($this->overridden) {
83+
trigger_deprecation('symfony/form', '7.2', 'Calling "%s::preSetData()" is deprecated, use "%s::postSetData()" instead.', static::class, __CLASS__);
84+
// parent::preSetData() has not been called, noop
85+
86+
return;
87+
}
88+
89+
if ($this->usePreSetData) {
90+
// nothing else to do
91+
return;
92+
}
93+
}
94+
5695
$form = $event->getForm();
5796
$data = $event->getData() ?? [];
5897

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

Lines changed: 2 additions & 2 deletions
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)
0