Skip to content

Commit

Permalink
[Form] Improve the exceptions when trying to get the data in a PRE_SE…
Browse files Browse the repository at this point in the history
…T_DATA listener and the data has not already been set
  • Loading branch information
fancyweb committed Mar 24, 2017
1 parent 80af083 commit ef39b70
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Symfony/Component/Form/Form.php
Expand Up @@ -408,6 +408,10 @@ public function getData()
}

if (!$this->defaultDataSet) {
if ($this->lockSetData) {
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.');
}

$this->setData($this->config->getData());
}

Expand All @@ -428,6 +432,10 @@ public function getNormData()
}

if (!$this->defaultDataSet) {
if ($this->lockSetData) {
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.');
}

$this->setData($this->config->getData());
}

Expand All @@ -448,6 +456,10 @@ public function getViewData()
}

if (!$this->defaultDataSet) {
if ($this->lockSetData) {
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.');
}

$this->setData($this->config->getData());
}

Expand Down
46 changes: 46 additions & 0 deletions src/Symfony/Component/Form/Tests/SimpleFormTest.php
Expand Up @@ -903,6 +903,7 @@ public function testViewDataMustBeObjectIfDataClassIsSet()

/**
* @expectedException \Symfony\Component\Form\Exception\RuntimeException
* @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.
*/
public function testSetDataCannotInvokeItself()
{
Expand Down Expand Up @@ -1084,6 +1085,51 @@ public function testCustomOptionsResolver()
$fooType->setDefaultOptions($resolver);
}

/**
* @expectedException \Symfony\Component\Form\Exception\RuntimeException
* @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.
*/
public function testCannotCallGetDataInPreSetDataListenerIfDataHasNotAlreadyBeenSet()
{
$config = new FormConfigBuilder('name', 'stdClass', $this->dispatcher);
$config->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getForm()->getData();
});
$form = new Form($config);

$form->setData('foo');
}

/**
* @expectedException \Symfony\Component\Form\Exception\RuntimeException
* @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.
*/
public function testCannotCallGetNormDataInPreSetDataListener()
{
$config = new FormConfigBuilder('name', 'stdClass', $this->dispatcher);
$config->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getForm()->getNormData();
});
$form = new Form($config);

$form->setData('foo');
}

/**
* @expectedException \Symfony\Component\Form\Exception\RuntimeException
* @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.
*/
public function testCannotCallGetViewDataInPreSetDataListener()
{
$config = new FormConfigBuilder('name', 'stdClass', $this->dispatcher);
$config->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getForm()->getViewData();
});
$form = new Form($config);

$form->setData('foo');
}

protected function createForm()
{
return $this->getBuilder()->getForm();
Expand Down

0 comments on commit ef39b70

Please sign in to comment.