From af97d0339e28f778394d4606e8edb758fabe3999 Mon Sep 17 00:00:00 2001 From: Jonathan McLean Date: Wed, 3 May 2017 13:26:30 +1000 Subject: [PATCH] Fix error when data is excluded by cell_filter This seems to affect only some old Symfony versions --- .../CheckboxRowCreationListener.php | 32 ++++++++++--------- Tests/CheckboxGrid/EntityCheckboxGridTest.php | 25 +++++++++++++++ 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Form/EventListener/CheckboxRowCreationListener.php b/Form/EventListener/CheckboxRowCreationListener.php index 29142e9..a060b4f 100644 --- a/Form/EventListener/CheckboxRowCreationListener.php +++ b/Form/EventListener/CheckboxRowCreationListener.php @@ -86,23 +86,25 @@ protected function addCheckbox($options, $choice, FormInterface $form, $value, $ { if (isset($options['cell_filter']) && !$options['cell_filter']($choice, $options['row'])) { // Blank cell - put a dummy form control here - $form->add($value, LegacyFormUtil::getType('Symfony\Component\Form\Extension\Core\Type\FormType'), array()); + $formType = LegacyFormUtil::getType('Symfony\Component\Form\Extension\Core\Type\FormType'); } else { - $builder = $this->factory->createNamedBuilder( - $value, - LegacyFormUtil::getType('Symfony\Component\Form\Extension\Core\Type\CheckboxType'), - isset($data[$value]), - array( - 'auto_initialize' => false, - 'required' => false, - ) - ); - - if (isset($data[$value])) { - $builder->addViewTransformer(new AnythingToBooleanTransformer($data[$value]), true); - } + $formType = LegacyFormUtil::getType('Symfony\Component\Form\Extension\Core\Type\CheckboxType'); + } - $form->add($builder->getForm()); + $builder = $this->factory->createNamedBuilder( + $value, + $formType, + isset($data[$value]), + array( + 'auto_initialize' => false, + 'required' => false, + ) + ); + + if (isset($data[$value])) { + $builder->addViewTransformer(new AnythingToBooleanTransformer($data[$value]), true); } + + $form->add($builder->getForm()); } } diff --git a/Tests/CheckboxGrid/EntityCheckboxGridTest.php b/Tests/CheckboxGrid/EntityCheckboxGridTest.php index 7133136..f5409fa 100644 --- a/Tests/CheckboxGrid/EntityCheckboxGridTest.php +++ b/Tests/CheckboxGrid/EntityCheckboxGridTest.php @@ -166,6 +166,31 @@ public function testEntityPreserved() $this->assertSame($spa, $salesman->getProductAreas()->first()); } + /** + * Test that cell_filter tolerates existing data. + */ + public function testCellFilter() + { + $this->expectSpa(); + + $salesman = new TestEntity\Salesman(); + + $salesman->addProductArea($spa = new TestEntity\SalesmanProductArea()); + $spa->setProductSold($this->products[2]); // Desk + $spa->setAreaServiced($this->areas[1]); // North side + + // This should run without throwing an error + $form = $this->factory->create(LegacyFormUtil::getType('Infinite\FormBundle\Tests\CheckboxGrid\Type\SalesmanType'), $salesman, array( + 'product_area_options' => array( + 'cell_filter' => function (TestEntity\Product $x, TestEntity\Area $y) { + return !( + $x === $this->products[2] && $y === $this->areas[1] + ); + }, + ), + )); + } + /** * Query builders are allowed on both axes. */