8000 bug #22140 [Form] Improve the exceptions when trying to get the data … · symfony/symfony@6008489 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6008489

Browse files
committed
bug #22140 [Form] Improve the exceptions when trying to get the data in a PRE_SET_DATA listener and the data has not already been set (fancyweb)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] Improve the exceptions when trying to get the data in a PRE_SET_DATA listener and the data has not already been set | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22103 | License | MIT | Doc PR | - Commits ------- ef39b70 [Form] Improve the exceptions when trying to get the data in a PRE_SET_DATA listener and the data has not already been set
2 parents d62b765 + ef39b70 commit 6008489

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/Symfony/Component/Form/Form.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ public function getData()
408408
}
409409

410410
if (!$this->defaultDataSet) {
411+
if ($this->lockSetData) {
412+
throw new RuntimeException('A cycle was detected. Listeners to the PRE_SET_DATA event must not call getData() if the form data has not already been set. You should call getData() on the FormEvent object instead.');
413+
}
414+
411415
$this->setData($this->config->getData());
412416
}
413417

@@ -428,6 +432,10 @@ public function getNormData()
428432
}
429433

430434
if (!$this->defaultDataSet) {
435+
if ($this->lockSetData) {
436+
throw new RuntimeException('A cycle was detected. Listeners to the PRE_SET_DATA event must not call getNormData() if the form data has not already been set.');
437+
}
438+
431439
$this->setData($this->config->getData());
432440
}
433441

@@ -448,6 +456,10 @@ public function getViewData()
448456
}
449457

450458
if (!$this->defaultDataSet) {
459+
if ($this->lockSetData) {
460+
throw new RuntimeException('A cycle was detected. Listeners to the PRE_SET_DATA event must not call getViewData() if the form data has not already been set.');
461+
}
462+
451463
$this->setData($this->config->getData());
452464
}
453465

src/Symfony/Component/Form/Tests/SimpleFormTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,7 @@ public function testViewDataMustBeObjectIfDataClassIsSet()
903903

904904
/**
905905
* @expectedException \Symfony\Component\Form\Exception\RuntimeException
906+
* @expectedExceptionMessage A cycle was detected. Listeners to the PRE_SET_DATA event must not call setData(). You should call setData() on the FormEvent object instead.
906907
*/
907908
public function testSetDataCannotInvokeItself()
908909
{
@@ -1084,6 +1085,51 @@ public function testCustomOptionsResolver()
10841085
$fooType->setDefaultOptions($resolver);
10851086
}
10861087

1088+
/**
1089+
* @expectedException \Symfony\Component\Form\Exception\RuntimeException
1090+
* @expectedExceptionMessage A cycle was detected. Listeners to the PRE_SET_DATA event must not call getData() if the form data has not already been set. You should call getData() on the FormEvent object instead.
1091+
*/
1092+
public function testCannotCallGetDataInPreSetDataListenerIfDataHasNotAlreadyBeenSet()
1093+
{
1094+
$config = new FormConfigBuilder('name', 'stdClass', $this->dispatcher);
1095+
$config->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
1096+
$event->getForm()->getData();
1097+
});
1098+
$form = new Form($config);
1099+
1100+
$form->setData('foo');
1101+
}
1102+
1103+
/**
1104+
* @expectedException \Symfony\Component\Form\Exception\RuntimeException
1105+
* @expectedExceptionMessage A cycle was detected. Listeners to the PRE_SET_DATA event must not call getNormData() if the form data has not already been set.
1106+
*/
1107+
public function testCannotCallGetNormDataInPreSetDataListener()
1108+
{
1109+
$config = new FormConfigBuilder('name', 'stdClass', $this->dispatcher);
1110+
$config->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
1111+
$event->getForm()->getNormData();
1112+
});
1113+
$form = new Form($config);
1114+
1115+
$form->setData('foo');
1116+
}
1117+
1118+
/**
1119+
* @expectedException \Symfony\Component\Form\Exception\RuntimeException
1120+
* @expectedExceptionMessage A cycle was detected. Listeners to the PRE_SET_DATA event must not call getViewData() if the form data has not already been set.
1121+
*/
1122+
public function testCannotCallGetViewDataInPreSetDataListener()
1123+
{
1124+
$config = new FormConfigBuilder('name', 'stdClass', $this->dispatcher);
1125+
$config->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
1126+
$event->getForm()->getViewData();
1127+
});
1128+
$form = new Form($config);
1129+
1130+
$form->setData('foo');
1131+
}
1132+
10871133
protected function createForm()
10881134
{
10891135
return $this->getBuilder()->getForm();

0 commit comments

Comments
 (0)
0