Skip to content

Commit

Permalink
Add regression test for filter
Browse files Browse the repository at this point in the history
Filters are used for converting an input value (usually a string)
into a domain specific PHP type and they are bound to the validation scope:
https://doc.nette.org/en/forms/validation#toc-modifying-input-values

45ed76a started filtering components
passed to `Container::validate()` to `Control`s, to appease PHPStan.
But `Multiplier` does not actually have `Control`s as direct children
(other than the ‘Add’ `Submitter`s), so it would stop validating and
filtering multiplied controls.

It has been since fixed in a5a7348,
and more properly in the parent commit but let’s add a test so this
does not happen again.

It can be reproduced by removing `|| $component instanceof Container`
from the parent commit.
  • Loading branch information
jtojnar committed Apr 17, 2024
1 parent 319362e commit 6932abf
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/Unit/MultiplierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,50 @@ public function testSendNestedInnerWithDefault()
);
}

/**
* Ensure filters work on submit, since they are dependent on properly set valdation scope.
* Regression test for https://github.com/contributte/forms-multiplier/issues/68
*/
public function testSubmitFilter()
{
$response = $this->services->form->createRequest(
MultiplierBuilder::create()
->fields([])
->beforeFormModifier(function (Form $form) {
$form->addInteger('num');
})
->multiplierModifier(function (Multiplier $multiplier) {
$multiplier->onCreate[] = function (Container $container) {
$container->addInteger('mnum');
};
})
->formModifier(function (Form $form) {
$form->onSuccess[] = $form->onError[] = $form->onSubmit[] = function () {
};
})
->createForm()
)
->setPost([
'num' => '11',
'm' => [
['mnum' => '49'],
],
])->send();

$this->assertTrue($response->isSuccess());
$this->assertSame([
'num' => 11,
'm' => [
['mnum' => 49],
],
], $response->getValues());

$dom = $response->toDomQuery();

$this->assertDomHas($dom, 'input[name="m[0][mnum]"][value="49"]');
$this->assertDomNotHas($dom, 'input[name="m[1][mnum]"]');
}

public function testGroup()
{
$request = $this->services->form->createRequest(
Expand Down

0 comments on commit 6932abf

Please sign in to comment.