Skip to content

Commit c494ece

Browse files
committed
Filter processing should not ask for values of whole form
1 parent 584d41c commit c494ece

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

src/Datagrid.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -1262,8 +1262,6 @@ public function filterSucceeded(NetteForm $form): void
12621262
return;
12631263
}
12641264

1265-
$values = (array) $form->getUntrustedValues(null);
1266-
12671265
if ($this->getPresenterInstance()->isAjax()) {
12681266
if (isset($form['group_action']['submit']) && $form['group_action']['submit']->isSubmittedBy()) {
12691267
return;
@@ -1273,9 +1271,11 @@ public function filterSucceeded(NetteForm $form): void
12731271
/**
12741272
* Per page
12751273
*/
1276-
if (isset($values['perPage'])) {
1277-
$this->saveSessionData('_grid_perPage', $values['perPage']);
1278-
$this->perPage = $values['perPage'];
1274+
$perPage = $form['perPage']->getValue();
1275+
1276+
if (isset($perPage)) {
1277+
$this->saveSessionData('_grid_perPage', $perPage);
1278+
$this->perPage = $perPage;
12791279
}
12801280

12811281
/**
@@ -1303,7 +1303,7 @@ public function filterSucceeded(NetteForm $form): void
13031303
$primaryWhereColumn = $form->getHttpData(Form::DataLine, 'inline_edit[_primary_where_column]');
13041304

13051305
if ($edit['submit']->isSubmittedBy() && $edit->getErrors() === []) {
1306-
$this->inlineEdit->onSubmit($id, $values['inline_edit']);
1306+
$this->inlineEdit->onSubmit($id, $form['inline_edit']->getValues());
13071307
$this->getPresenterInstance()->payload->_datagrid_inline_edited = $id;
13081308
$this->getPresenterInstance()->payload->_datagrid_name = $this->getFullName();
13091309
} else {
@@ -1345,7 +1345,7 @@ public function filterSucceeded(NetteForm $form): void
13451345

13461346
if ($add['submit']->isSubmittedBy() || $add['cancel']->isSubmittedBy()) {
13471347
if ($add['submit']->isSubmittedBy() && $add->getErrors() === []) {
1348-
$this->inlineAdd->onSubmit($values['inline_add']);
1348+
$this->inlineAdd->onSubmit($form['inline_add']->getValues());
13491349
}
13501350

13511351
$this->redrawControl('tbody');
@@ -1359,7 +1359,7 @@ public function filterSucceeded(NetteForm $form): void
13591359
/**
13601360
* Filter itself
13611361
*/
1362-
$values = $values['filter'];
1362+
$values = $form['filter']->getValues();
13631363

13641364
if (!$values instanceof ArrayHash) {
13651365
throw new UnexpectedValueException();

tests/Cases/FilterTest.phpt

+34
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ namespace Contributte\Datagrid\Tests\Cases;
55
require __DIR__ . '/../bootstrap.php';
66

77
use Contributte\Datagrid\Datagrid;
8+
use Contributte\Datagrid\Tests\Files\FormValueObject;
89
use Contributte\Datagrid\Tests\Files\TestingDatagridFactoryRouter;
910
use Nette\Application\AbortException;
11+
use Nette\Forms\Container;
1012
use Tester\Assert;
1113
use Tester\TestCase;
1214

@@ -25,6 +27,38 @@ final class FilterTest extends TestCase
2527
}, AbortException::class);
2628
}
2729

30+
/**
31+
* This case is testing grid filter processing to not cause side effects by unnecessarily instantiating
32+
* value object {@see FormValueObject} of inline add form container. This value object is crafted
33+
* to fail on constructor argument type check due to inline add form container not being validated in this case.
34+
*/
35+
public function testFilterSubmitWithInvalidInlineAddOpen(): void
36+
{
37+
$factory = new TestingDataGridFactoryRouter();
38+
/** @var \Ublaboo\DataGrid\Datagrid $grid */
39+
$grid = $factory->createTestingDataGrid()->getComponent('grid');
40+
41+
$grid->addColumnText('status', 'Status');
42+
43+
$grid->addInlineAdd()->onControlAdd[] = function (Container $container) {
44+
$container->setMappedType(FormValueObject::class);
45+
$container->addSelect('status', '', [
46+
// items are irrelevant, case is testing control returning null value
47+
1 => 'Concept',
48+
2 => 'Active',
49+
3 => 'Unpublished',
50+
])
51+
->setPrompt('---')
52+
->setRequired();
53+
};
54+
55+
$filterForm = $grid->createComponentFilter();
56+
57+
Assert::exception(function() use ($grid, $filterForm): void {
58+
$grid->filterSucceeded($filterForm);
59+
}, AbortException::class);
60+
}
61+
2862
}
2963

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

tests/Files/FormValueObject.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Contributte\Datagrid\Tests\Files;
5+
6+
class FormValueObject
7+
{
8+
9+
public int $status;
10+
11+
}

0 commit comments

Comments
 (0)