Skip to content

[Form] added ChildDataEvent #3768

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

Closed
wants to merge 3 commits into from
Closed
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
48 changes: 48 additions & 0 deletions src/Symfony/Component/Form/Event/ChildDataEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Event;

use Symfony\Component\Form\FormInterface;

class ChildDataEvent extends DataEvent
{
private $name;

/**
* Constructs an event.
*
* @param FormInterface $form The associated form
* @param string $name The name of the field that was just set
* @param mixed $data The data of the field that was just set
*/
public function __construct(FormInterface $form, $name, $data)
{
$this->name = $name;
parent::__construct($form, $data);
}

/**
* Gets the data of the field that was just set
*/
public function getData()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method is totally useless as it does not change anything from the parent method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, hence my comment, but you beat me to it ;) The only reason I added it was for the docblock

{
return parent::getData();
}

/**
* Gets the name of the field that was updated
*/
public function getName()
{
return $this->name;
}
}
21 changes: 15 additions & 6 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\Event\ChildDataEvent;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\Form\Exception\AlreadyBoundException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
Expand Down Expand Up @@ -506,13 +507,21 @@ public function bind($clientData)
}
}

foreach ($clientData as $name => $value) {
if ($this->has($name)) {
$this->children[$name]->bind($value);
} else {
$extraData[$name] = $value;
$extraData = $clientData;
do {
$lastChildren = $this->children;
foreach ($extraData as $name => $value) {
if ($this->has($name)) {
$this->children[$name]->bind($value);
unset($extraData[$name]);

// every time we bind a child, we dispatch an event to allow
// listeners to add or remove field based on the result value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: a field or fields

$event = new ChildDataEvent($this, $name, $this->children[$name]->getData());
$this->dispatcher->dispatch(FormEvents::BIND_CHILD, $event);
}
}
}
} while(count($extraData) && $lastChildren !== $this->children);

// If we have a data mapper, use old client data and merge
// data from the children into it later
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/FormEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ final class FormEvents

const BIND_CLIENT_DATA = 'form.bind_client_data';

const BIND_CHILD = 'form.bind_child';

const BIND_NORM_DATA = 'form.bind_norm_data';

const SET_DATA = 'form.set_data';
Expand Down
74 changes: 74 additions & 0 deletions src/Symfony/Component/Form/Tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer;
use Symfony\Component\Form\Tests\Fixtures\FixedFilterListener;
use Symfony\Component\Form\Event\ChildDataEvent;
use Symfony\Component\Form\FormEvents;

class FormTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -1303,6 +1305,78 @@ public function testGetValidatorsReturnsValidators()

$this->assertEquals(array($validator), $form->getValidators());
}

public function testBindChild()
{
$field1builder = $this->getBuilder('field1');
$field2builder = $this->getBuilder('field2');

$builder = $this->getBuilder('form', new EventDispatcher())
->addEventListener(FormEvents::BIND_CHILD, function(ChildDataEvent $e) use ($field2builder) {
if ($e->getName() == 'field1') {
if ($e->getData() == 'a') {
$e->getForm()->add($field2builder->getForm());
} else {
$e->getForm()->remove('field2');
}
}
});
$form = $builder->getForm();
$form->add($field1builder->getForm());

$form->bind(array('field1'=>'a', 'field2'=>'b'));
$this->assertTrue($form->has('field2'));
$this->assertEmpty($form->getExtraData());

$form = $builder->getForm();
$form->add($field1builder->getForm());
$form->add($field2builder->getForm());

$form->bind(array('field1'=>'b', 'field2'=>'a'));
$this->assertFalse($form->has('field2'));
$this->assertEquals(array('field2'=>'a'), $form->getExtraData());
}

public function testBindChildDoesntResultInEndlessLoop()
{
$field1builder = $this->getBuilder('field1');

$builder = $this->getBuilder('form');

$form = $builder->getForm();
$form->add($field1builder->getForm());

$form->bind(array('field1'=>'a', 'field2'=>'b'));
}

public function testBindChildKeepsLoopingUntilNoFieldsAreAdded()
{
$transformer = $this->getDataTransformer();
$transformer->expects($this->exactly(3))->method('reverseTransform')->will($this->onConsecutiveCalls(array('abound', 'bbound', 'cbound')));

$field2builder = $this->getBuilder('field2')->appendClientTransformer($transformer);
$field3builder = $this->getBuilder('field3')->appendClientTransformer($transformer);

$builder = $this->getBuilder('form', new EventDispatcher())
->addEventListener(FormEvents::BIND_CHILD, function(ChildDataEvent $e) use ($field2builder, $field3builder) {
if ($e->getName() == 'field1') {
$e->getForm()->add($field2builder->getForm());
} elseif ($e->getName() == 'field2') {
$e->getForm()->add($field3builder->getForm());
}
});

$form = $builder->getForm();

$form->add($this->getBuilder('field1')->appendClientTransformer($transformer)->getForm());

$form->bind(array('field1'=>'a', 'field2'=>'b', 'field3'=>'c', 'field4'=>'d'));

// issue #3770
//$this->assertEquals(array('field1'=>'abound', 'field2'=>'bbound', 'field3'=>'cbound', 'field4'=>'d'), $form->getData());
$this->assertEquals(array('field1'=>'a', 'field2'=>'b', 'field3'=>'c', 'field4'=>'d'), $form->getData());
$this->assertEquals(array('field4'=>'d'), $form->getExtraData());
}

protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null)
{
Expand Down