Skip to content

Commit

Permalink
Fix error when data is excluded by cell_filter
Browse files Browse the repository at this point in the history
This seems to affect only some old Symfony versions
  • Loading branch information
Jonathan McLean committed May 3, 2017
1 parent 2c70665 commit af97d03
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
32 changes: 17 additions & 15 deletions Form/EventListener/CheckboxRowCreationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
25 changes: 25 additions & 0 deletions Tests/CheckboxGrid/EntityCheckboxGridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit af97d03

Please sign in to comment.