From 41429bd4cb0851a6dae0290174d38a8154cb8866 Mon Sep 17 00:00:00 2001 From: GeLo Date: Sat, 12 Dec 2015 19:00:31 +0100 Subject: [PATCH] [ResolvedFormType] Detect children view dynamically removed --- src/OrderedResolvedFormType.php | 4 ++ ...ion.php => ExtraChildrenViewExtension.php} | 2 +- .../Fixtures/RemoveChildrenViewExtension.php | 61 +++++++++++++++++++ tests/OrderedFormFunctionnalTest.php | 25 +++++++- 4 files changed, 88 insertions(+), 4 deletions(-) rename tests/Fixtures/{ExtraViewChildrenExtension.php => ExtraChildrenViewExtension.php} (95%) create mode 100644 tests/Fixtures/RemoveChildrenViewExtension.php diff --git a/src/OrderedResolvedFormType.php b/src/OrderedResolvedFormType.php index b9f5bc8..951d932 100644 --- a/src/OrderedResolvedFormType.php +++ b/src/OrderedResolvedFormType.php @@ -60,6 +60,10 @@ public function finishView(FormView $view, FormInterface $form, array $options) $view->children = array(); foreach ($this->orderer->order($form) as $name) { + if (!isset($children[$name])) { + continue; + } + $view->children[$name] = $children[$name]; unset($children[$name]); } diff --git a/tests/Fixtures/ExtraViewChildrenExtension.php b/tests/Fixtures/ExtraChildrenViewExtension.php similarity index 95% rename from tests/Fixtures/ExtraViewChildrenExtension.php rename to tests/Fixtures/ExtraChildrenViewExtension.php index af7cc78..cd02ea7 100644 --- a/tests/Fixtures/ExtraViewChildrenExtension.php +++ b/tests/Fixtures/ExtraChildrenViewExtension.php @@ -20,7 +20,7 @@ * * @author GeLo */ -class ExtraViewChildrenExtension extends AbstractTypeExtension +class ExtraChildrenViewExtension extends AbstractTypeExtension { /** @var array */ private $names; diff --git a/tests/Fixtures/RemoveChildrenViewExtension.php b/tests/Fixtures/RemoveChildrenViewExtension.php new file mode 100644 index 0000000..4609e1d --- /dev/null +++ b/tests/Fixtures/RemoveChildrenViewExtension.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Ivory\Tests\OrderedForm\Fixtures; + +use Symfony\Component\Form\AbstractTypeExtension; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\Form\FormView; + +/** + * Extra view children extension. + * + * @author GeLo + */ +class RemoveChildrenViewExtension extends AbstractTypeExtension +{ + /** @var array */ + private $names; + + /** + * Creates an extra view children extension. + * + * @param array $names The extra view names. + */ + public function __construct(array $names) + { + $this->names = $names; + } + + /** + * {@inheritdoc} + */ + public function finishView(FormView $view, FormInterface $form, array $options) + { + if ($form->isRoot()) { + return; + } + + foreach ($this->names as $name) { + unset($view[$name]); + } + } + + /** + * {@inheritdoc} + */ + public function getExtendedType() + { + return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') + ? 'Symfony\Component\Form\Extension\Core\Type\FormType' + : 'form'; + } +} diff --git a/tests/OrderedFormFunctionnalTest.php b/tests/OrderedFormFunctionnalTest.php index 859854c..8ffd7bb 100644 --- a/tests/OrderedFormFunctionnalTest.php +++ b/tests/OrderedFormFunctionnalTest.php @@ -13,7 +13,8 @@ use Ivory\OrderedForm\Extension\OrderedExtension; use Ivory\OrderedForm\OrderedResolvedFormTypeFactory; -use Ivory\Tests\OrderedForm\Fixtures\ExtraViewChildrenExtension; +use Ivory\Tests\OrderedForm\Fixtures\ExtraChildrenViewExtension; +use Ivory\Tests\OrderedForm\Fixtures\RemoveChildrenViewExtension; use Symfony\Component\Form\Forms; use Symfony\Component\Form\FormView; @@ -74,14 +75,14 @@ public function testInvalidPosition(array $config, $exceptionMessage = null) $this->createForm($config)->createView(); } - public function testExtraViewChild() + public function testExtraChildrenView() { $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ? 'Symfony\Component\Form\Extension\Core\Type\FormType' : 'form'; $view = $this->factoryBuilder - ->addTypeExtension(new ExtraViewChildrenExtension(array('extra1', 'extra2'))) + ->addTypeExtension(new ExtraChildrenViewExtension(array('extra1', 'extra2'))) ->getFormFactory() ->createBuilder() ->add('foo', $type, array('position' => 'last')) @@ -92,6 +93,24 @@ public function testExtraViewChild() $this->assertPositions($view, array('bar', 'foo', 'extra1', 'extra2')); } + public function testRemoveChildrenView() + { + $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') + ? 'Symfony\Component\Form\Extension\Core\Type\FormType' + : 'form'; + + $view = $this->factoryBuilder + ->addTypeExtension(new RemoveChildrenViewExtension(array('foo'))) + ->getFormFactory() + ->createBuilder() + ->add('foo', $type, array('position' => 'last')) + ->add('bar', $type, array('position' => 'first')) + ->getForm() + ->createView(); + + $this->assertPositions($view, array('bar')); + } + /** * Gets the valid positions. *