Skip to content

Commit 37830e8

Browse files
feature #51043 [Form] Deprecate FormEvent::setData() for events that do not allow it (HeahDude)
This PR was merged into the 6.4 branch. Discussion ---------- [Form] Deprecate `FormEvent::setData()` for events that do not allow it | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | no | Deprecations? | yes | Tickets | ~ | License | MIT | Doc PR | TODO Now that specific classes exist for each event, we can improve DX by forbidding to call `FormEvent::setData()` where it does not make sense. Commits ------- 0d318d0502 [Form] Deprecate `FormEvent::setData()` for events that do not allow it
2 parents 2aeb717 + 3673459 commit 37830e8

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

CHANGELOG.md

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

77
* Deprecate using `DateTime` or `DateTimeImmutable` model data with a different timezone than configured with the
88
`model_timezone` option in `DateType`, `DateTimeType`, and `TimeType`
9+
* Deprecate `PostSetDataEvent::setData()`, use `PreSetDataEvent::setData()` instead
10+
* Deprecate `PostSubmitEvent::setData()`, use `PreSubmitDataEvent::setData()` or `SubmitDataEvent::setData()` instead
911

1012
6.3
1113
---

Event/PostSetDataEvent.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Event;
1313

14+
use Symfony\Component\Form\Exception\BadMethodCallException;
1415
use Symfony\Component\Form\FormEvent;
1516

1617
/**
@@ -20,4 +21,12 @@
2021
*/
2122
final class PostSetDataEvent extends FormEvent
2223
{
24+
/**
25+
* @deprecated since Symfony 6.4, it will throw an exception in 7.0.
26+
*/
27+
public function setData(mixed $data): void
28+
{
29+
trigger_deprecation('symfony/form', '6.4', 'Calling "%s()" will throw an exception as of 7.0, listen to "form.pre_set_data" instead.', __METHOD__);
30+
// throw new BadMethodCallException('Form data cannot be changed during "form.post_set_data", you should use "form.pre_set_data" instead.');
31+
}
2332
}

Event/PostSubmitEvent.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,12 @@
2121
*/
2222
final class PostSubmitEvent extends FormEvent
2323
{
24+
/**
25+
* @deprecated since Symfony 6.4, it will throw an exception in 7.0.
26+
*/
27+
public function setData(mixed $data): void
28+
{
29+
trigger_deprecation('symfony/form', '6.4', 'Calling "%s()" will throw an exception as of 7.0, listen to "form.pre_submit" or "form.submit" instead.', __METHOD__);
30+
// throw new BadMethodCallException('Form data cannot be changed during "form.post_submit", you should use "form.pre_submit" or "form.submit" instead.');
31+
}
2432
}

0 commit comments

Comments
 (0)