Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,32 @@
*/
class MergeDoctrineCollectionListener implements EventSubscriberInterface
{
// Keeps BC. To be removed in 4.0
// Keep BC. To be removed in 4.0
private $bc = true;
private $bcLayer = false;

public static function getSubscribedEvents()
{
// Higher priority than core MergeCollectionListener so that this one
// is called before
return array(
FormEvents::SUBMIT => array(
// BC
array('onBind', 10),
array('onBind', 10), // deprecated
array('onSubmit', 5),
),
);
}

public function onSubmit(FormEvent $event)
{
// If onBind() is overridden then logic has been executed
if ($this->bc) {
// onBind() has been overridden from a child class
@trigger_error('The onBind() method is deprecated since version 3.1 and will be removed in 4.0. Use the onSubmit() method instead.', E_USER_DEPRECATED);

return;
if (!$this->bcLayer) {
// If parent::onBind() has not been called, then logic has been executed
return;
}
}

$collection = $event->getForm()->getData();
Expand All @@ -68,10 +71,13 @@ public function onSubmit(FormEvent $event)
* @deprecated since version 3.1, to be removed in 4.0.
* Use {@link onSubmit()} instead.
*/
public function onBind()
public function onBind(FormEvent $event)
{
if (__CLASS__ === get_class($this)) {
$this->bc = false;
} else {
// parent::onBind() has been called
$this->bcLayer = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

class MergeDoctrineCollectionTest extends \PHPUnit_Framework_TestCase
class MergeDoctrineCollectionListenerTest extends \PHPUnit_Framework_TestCase
{
/** @var \Doctrine\Common\Collections\ArrayCollection */
private $collection;
Expand Down Expand Up @@ -77,4 +77,37 @@ public function testOnSubmitNullClearCollection()

$this->assertTrue($this->collection->isEmpty());
}

/**
* @group legacy
*/
public function testLegacyChildClassOnSubmitCallParent()
{
$form = $this->getBuilder('name')
->setData($this->collection)
->addEventSubscriber(new TestClassExtendingMergeDoctrineCollectionListener())
->getForm();
$submittedData = array();
$event = new FormEvent($form, $submittedData);

$this->dispatcher->dispatch(FormEvents::SUBMIT, $event);

$this->assertTrue($this->collection->isEmpty());
$this->assertTrue(TestClassExtendingMergeDoctrineCollectionListener::$onBindCalled);
}
}

/**
* @group legacy
*/
class TestClassExtendingMergeDoctrineCollectionListener extends MergeDoctrineCollectionListener
{
public static $onBindCalled = false;

public function onBind(FormEvent $event)
{
self::$onBindCalled = true;

parent::onBind($event);
}
}