Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiplier::validate(): Filter out non-validatable components #103

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 1 addition & 3 deletions src/Multiplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@
*/
public function validate(?array $controls = null): void
{
/** @var Control[] $components */
$components = $controls ?? $this->getComponents();
$components = $controls ?? array_filter($this->getComponents(), fn ($component) => $component instanceof Control || $component instanceof Container);

Check failure on line 173 in src/Multiplier.php

View workflow job for this annotation

GitHub Actions / Phpstan / Phpstan (8.1)

Parameter #1 $array of function array_filter expects array, Iterator<int|string, Nette\ComponentModel\IComponent> given.

Check failure on line 173 in src/Multiplier.php

View workflow job for this annotation

GitHub Actions / Phpstan / Phpstan (8.1)

Parameter #1 $array of function array_filter expects array, Iterator<int|string, Nette\ComponentModel\IComponent> given.

foreach ($components as $index => $control) {
foreach ($this->noValidate as $item) {
Expand Down Expand Up @@ -313,8 +312,7 @@
{
$this->createCopies();

/** @var array<int|string,Container> $containers */
$containers = array_filter($this->getComponents(), fn ($component) => $component instanceof Container);

Check failure on line 315 in src/Multiplier.php

View workflow job for this annotation

GitHub Actions / Phpstan / Phpstan (8.1)

Parameter #1 $array of function array_filter expects array, Iterator<int|string, Nette\ComponentModel\IComponent> given.

Check failure on line 315 in src/Multiplier.php

View workflow job for this annotation

GitHub Actions / Phpstan / Phpstan (8.1)

Parameter #1 $array of function array_filter expects array, Iterator<int|string, Nette\ComponentModel\IComponent> given.

return $containers;
}
Expand Down Expand Up @@ -392,7 +390,7 @@

protected function createNumber(): int
{
$count = count(array_filter($this->getComponents(), fn ($component) => $component instanceof Form));

Check failure on line 393 in src/Multiplier.php

View workflow job for this annotation

GitHub Actions / Phpstan / Phpstan (8.1)

Parameter #1 $array of function array_filter expects array, Iterator<int|string, Nette\ComponentModel\IComponent> given.

Check failure on line 393 in src/Multiplier.php

View workflow job for this annotation

GitHub Actions / Phpstan / Phpstan (8.1)

Parameter #1 $array of function array_filter expects array, Iterator<int|string, Nette\ComponentModel\IComponent> given.
while ($this->getComponent((string) $count, false)) {
$count++;
}
Expand Down Expand Up @@ -428,7 +426,7 @@
*/
protected function getFirstSubmit(): ?string
{
$submits = array_filter($this->getComponents(), fn ($component) => $component instanceof SubmitButton);

Check failure on line 429 in src/Multiplier.php

View workflow job for this annotation

GitHub Actions / Phpstan / Phpstan (8.1)

Parameter #1 $array of function array_filter expects array, Iterator<int|string, Nette\ComponentModel\IComponent> given.

Check failure on line 429 in src/Multiplier.php

View workflow job for this annotation

GitHub Actions / Phpstan / Phpstan (8.1)

Parameter #1 $array of function array_filter expects array, Iterator<int|string, Nette\ComponentModel\IComponent> given.
if ($submits) {
return reset($submits)->getName();
}
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/MultiplierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,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
Loading