1212namespace Symfony \Component \Form \Extension \Core \EventListener ;
1313
1414use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
15+ use Symfony \Component \Form \Event \PostSetDataEvent ;
1516use Symfony \Component \Form \Exception \UnexpectedTypeException ;
1617use Symfony \Component \Form \FormEvent ;
1718use 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
0 commit comments