Skip to content

Commit

Permalink
Merge pull request #14 from egeloen/issue-10
Browse files Browse the repository at this point in the history
[ResolvedFormType] Detect children view dynamically removed
  • Loading branch information
GeLoLabs committed Dec 12, 2015
2 parents 5f2bd2d + 41429bd commit 7a6bbfb
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/OrderedResolvedFormType.php
Expand Up @@ -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]);
}
Expand Down
Expand Up @@ -20,7 +20,7 @@
*
* @author GeLo <geloen.eric@gmail.com>
*/
class ExtraViewChildrenExtension extends AbstractTypeExtension
class ExtraChildrenViewExtension extends AbstractTypeExtension
{
/** @var array */
private $names;
Expand Down
61 changes: 61 additions & 0 deletions tests/Fixtures/RemoveChildrenViewExtension.php
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Ivory Ordered Form package.
*
* (c) Eric GELOEN <geloen.eric@gmail.com>
*
* 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 <geloen.eric@gmail.com>
*/
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';
}
}
25 changes: 22 additions & 3 deletions tests/OrderedFormFunctionnalTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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'))
Expand All @@ -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.
*
Expand Down

0 comments on commit 7a6bbfb

Please sign in to comment.