12
12
namespace Symfony \Component \Form \Extension \Core \EventListener ;
13
13
14
14
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
15
+ use Symfony \Component \Form \Event \PostSetDataEvent ;
15
16
use Symfony \Component \Form \Exception \UnexpectedTypeException ;
16
17
use Symfony \Component \Form \FormEvent ;
17
18
use Symfony \Component \Form \FormEvents ;
@@ -27,6 +28,9 @@ class ResizeFormListener implements EventSubscriberInterface
27
28
protected array $ prototypeOptions ;
28
29
29
30
private \Closure |bool $ deleteEmpty ;
31
+ // BC, to be removed in 8.0
32
+ private bool $ overridden = true ;
33
+ private bool $ usePreSetData = false ;
30
34
31
35
public function __construct (
32
36
private string $ type ,
@@ -44,15 +48,57 @@ public function __construct(
44
48
public static function getSubscribedEvents (): array
45
49
{
46
50
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
48
53
FormEvents::PRE_SUBMIT => 'preSubmit ' ,
49
54
// (MergeCollectionListener, MergeDoctrineCollectionListener)
50
55
FormEvents::SUBMIT => ['onSubmit ' , 50 ],
51
56
];
52
57
}
53
58
59
+ /**
60
+ * @deprecated Since Symfony 7.2, use {@see postSetData()} instead.
61
+ */
54
62
public function preSetData (FormEvent $ event ): void
55
63
{
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
+
56
102
$ form = $ event ->getForm ();
57
103
$ data = $ event ->getData () ?? [];
58
104
0 commit comments