Skip to content

[Form] Add "reindex" option for the CollectionType #13116

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 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ class ResizeFormListener implements EventSubscriberInterface
*/
private $deleteEmpty;

public function __construct($type, array $options = array(), $allowAdd = false, $allowDelete = false, $deleteEmpty = false)
/**
* @var bool
*/
private $reindex;

public function __construct($type, array $options = array(), $allowAdd = false, $allowDelete = false, $deleteEmpty = false, $reindex = false)
{
$this->type = $type;
$this->allowAdd = $allowAdd;
$this->allowDelete = $allowDelete;
$this->options = $options;
$this->deleteEmpty = $deleteEmpty;
$this->reindex = $reindex;
}

public static function getSubscribedEvents()
Expand Down Expand Up @@ -110,6 +116,37 @@ public function preSubmit(FormEvent $event)
$data = array();
}

// Fix array indices
if ($this->reindex) {
$tempData = array();
foreach ($form as $name => $child) {
if (isset($data[$name])) {
$tempData[$name] = $data[$name];
unset($data[$name]);
} else {
$tempData[$name] = null;
}
}

$toDelete = array();
foreach ($data as $name => $child) {
$tempData[] = $child;
$toDelete[] = $name;
}

foreach ($toDelete as $name) {
unset($data[$name]);
}

foreach ($tempData as $name => $child) {
if (null !== $child) {
$data[$name] = $child;
}
}

$event->setData($data);
}

// Remove all empty rows
if ($this->allowDelete) {
foreach ($form as $name => $child) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$options['options'],
$options['allow_add'],
$options['allow_delete'],
$options['delete_empty']
$options['delete_empty'],
$options['reindex']
);

$builder->addEventSubscriber($resizeListener);
Expand Down Expand Up @@ -89,6 +90,7 @@ public function configureOptions(OptionsResolver $resolver)
'type' => 'text',
'options' => array(),
'delete_empty' => false,
'reindex' => false,
));

$resolver->setNormalizer('options', $optionsNormalizer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,73 @@ public function testOnSubmitDealsWithArrayBackedIteratorAggregate()
$this->assertArrayNotHasKey(0, $event->getData());
$this->assertArrayNotHasKey(2, $event->getData());
}

public function testPreSubmitReindexArray()
{
$this->form->add($this->getForm('0'));
$this->form->add($this->getForm('1'));
$this->form->add($this->getForm('2'));

$this->factory->expects($this->at(0))
->method('createNamed')
->with(3, 'text', null, array('property_path' => '[3]', 'attr' => array('maxlength' => 10), 'auto_initialize' => false))
->will($this->returnValue($this->getForm('3')));
$this->factory->expects($this->at(1))
->method('createNamed')
->with(4, 'text', null, array('property_path' => '[4]', 'attr' => array('maxlength' => 10), 'auto_initialize' => false))
->will($this->returnValue($this->getForm('4')));

$data = array(0 => 'first', 2 => 'second', 4 => 'third', 6 => 'fourth');
$event = new FormEvent($this->form, $data);
$listener = new ResizeFormListener('text', array('attr' => array('maxlength' => 10)), true, true, false, true);
$listener->preSubmit($event);

$this->assertEquals(array(0 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth'), $event->getData());
}

public function testPreSubmitReindexArrayWithMixedKeys()
{
$this->form->add($this->getForm('0'));
$this->form->add($this->getForm('x'));
$this->form->add($this->getForm('2'));

$this->factory->expects($this->at(0))
->method('createNamed')
->with(3, 'text', null, array('property_path' => '[3]', 'attr' => array('maxlength' => 10), 'auto_initialize' => false))
->will($this->returnValue($this->getForm('3')));
$this->factory->expects($this->at(1))
->method('createNamed')
->with(4, 'text', null, array('property_path' => '[4]', 'attr' => array('maxlength' => 10), 'auto_initialize' => false))
->will($this->returnValue($this->getForm('4')));

$data = array(0 => 'first', 'x' => 'second', 4 => 'third', 6 => 'fourth');
$event = new FormEvent($this->form, $data);
$listener = new ResizeFormListener('text', array('attr' => array('maxlength' => 10)), true, true, false, true);
$listener->preSubmit($event);

$this->assertEquals(array(0 => 'first', 'x' => 'second', 3 => 'third', 4 => 'fourth'), $event->getData());
}

public function testPreSubmitReindexArrayObject()
{
$this->form->add($this->getForm('0'));
$this->form->add($this->getForm('1'));
$this->form->add($this->getForm('2'));

$this->factory->expects($this->at(0))
->method('createNamed')
->with(3, 'text', null, array('property_path' => '[3]', 'attr' => array('maxlength' => 10), 'auto_initialize' => false))
->will($this->returnValue($this->getForm('3')));
$this->factory->expects($this->at(1))
->method('createNamed')
->with(4, 'text', null, array('property_path' => '[4]', 'attr' => array('maxlength' => 10), 'auto_initialize' => false))
->will($this->returnValue($this->getForm('4')));

$data = new \ArrayObject(array(0 => 'first', 2 => 'second', 4 => 'third', 6 => 'fourth'));
$event = new FormEvent($this->form, $data);
$listener = new ResizeFormListener('text', array('attr' => array('maxlength' => 10)), true, true, false, true);
$listener->preSubmit($event);

$this->assertEquals(new \ArrayObject(array(0 => 'first', 2 => 'second', 3 => 'third', 4 => 'fourth')), $event->getData());
}
}