Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/Filter/FilterSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ protected function addControl(
{
$input = $container->addSelect($key, $name, $options);

// SelectBox adds an implicit "Filled" rule when no prompt is set.
// A filter is never required, and when its column is hidden the input
// isn't rendered, so nothing is submitted and the rule fails — which
// invalidates the whole filter container and triggers a warning on
// getValues(). Filters don't need validation, so drop it here.
$input->getRules()->reset();

if ($this->getPrompt() !== null) {
$input->setPrompt($this->getPrompt());
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Cases/FilterTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,40 @@ final class FilterTest extends TestCase
Assert::count(0, $grid->filter);
}

/**
* Regression test for https://github.com/contributte/datagrid/issues/1281
*
* A FilterSelect on a hidden column isn't rendered in HTML, so no value is
* submitted for it. SelectBox's constructor adds an implicit "Filled" rule
* when no prompt is set, which used to fail validation for the missing
* value, invalidate the filter container, and trigger an E_USER_WARNING
* from Container::getValues() in Datagrid::filterSucceeded().
*/
public function testFilterSelectHasNoImplicitValidation(): void
{
$factory = new TestingDatagridFactoryRouter();
/** @var Datagrid $grid */
$grid = $factory->createTestingDatagrid()->getComponent('grid');

$grid->addColumnText('name', 'Name')
->setFilterText();

$grid->addColumnText('status', 'Status')
->setFilterSelect(['yes' => 'Yes', 'no' => 'No']);

$filterForm = $grid->createComponentFilter();

$filterContainer = $filterForm->getComponent('filter');
Assert::type(Container::class, $filterContainer);

$filterContainer->validate();

// With no value set (as if the select was hidden and not submitted),
// the container must still be valid — no implicit Filled rule should
// fire on the FilterSelect.
Assert::true($filterContainer->isValid());
}

}

(new FilterTest())->run();
Loading