-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Use form.post_set_data
in ResizeFormListener
#51041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Form] Use form.post_set_data
in ResizeFormListener
#51041
Conversation
src/Symfony/Component/Form/Extension/DataCollector/EventListener/DataCollectorListener.php
Show resolved
Hide resolved
0967026
to
8818732
Compare
src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php
Outdated
Show resolved
Hide resolved
8818732
to
ba36dd3
Compare
src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php
Outdated
Show resolved
Hide resolved
42ef705
to
7422f04
Compare
@@ -45,18 +50,52 @@ public function __construct(string $type, array $options = [], bool $allowAdd = | |||
public static function getSubscribedEvents(): array | |||
{ | |||
return [ | |||
FormEvents::PRE_SET_DATA => 'preSetData', | |||
FormEvents::PRE_SET_DATA => 'preSetData', // deprecated | |||
FormEvents::POST_SET_DATA => ['postSetData', 255], // as early as possible |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it actually allowed to modify the structure of the form on postSetData ? To me, this is too late as those child fields won't be populated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, the form is not cloned and can be modified by reference https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Form/Form.php#L327.
The only event that forbids it is POST_SUBMIT
thanks to this check https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Form/Form.php#L260, in such case $this->defaultDataSet
is true, but not enough, only data cannot further be changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, this is too late as those child fields won't be populated.
They will be: https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Form/Form.php#L776.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've refactored the tests to ensure data is mapped consistently: 6588362.
2662d0b
to
6588362
Compare
Needs a rebase, thanks |
Friendly up @HeahDude |
ca3acd9
to
dcf7a29
Compare
I have rebased the PR |
$this->postSetData($event); | ||
$this->usePreSetData = true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->postSetData($event); | |
$this->usePreSetData = true; | |
try { | |
$this->postSetData($event); | |
} finally { | |
$this->usePreSetData = true; | |
} |
} | ||
|
||
// Remove FormEvent type hint in 8.0 | ||
final public function postSetData(FormEvent|PostSetDataEvent $event): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@final since Symfony 7.3
insead
final public function postSetData(FormEvent|PostSetDataEvent $event): void | |
/** | |
* @param PostSetDataEvent $event | |
*/ | |
public function postSetData(FormEvent|PostSetDataEvent $event): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM after minor comments.
dcf7a29
to
768c6b4
Compare
768c6b4
to
de06a0f
Compare
Thank you Jules. |
Thank you Christian :). |
The
form.pre_set_data
event allows to change the initial data, modifying the form based on it should be done early duringform.post_set_data
instead.Ref symfony/symfony-docs#18587.
Also, the collect of form config should be done as late as possible to account for modification from other listeners.