diff --git a/src/Multiplier.php b/src/Multiplier.php index 8cf4c12..4f48ff6 100644 --- a/src/Multiplier.php +++ b/src/Multiplier.php @@ -170,8 +170,7 @@ public function addCreateButton(?string $caption = null, int $copyCount = 1): Cr */ 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); foreach ($components as $index => $control) { foreach ($this->noValidate as $item) { @@ -313,7 +312,6 @@ public function getContainers(): iterable { $this->createCopies(); - /** @var array $containers */ $containers = array_filter($this->getComponents(), fn ($component) => $component instanceof Container); return $containers; diff --git a/tests/Unit/MultiplierTest.php b/tests/Unit/MultiplierTest.php index bf04ca4..0a6f55e 100644 --- a/tests/Unit/MultiplierTest.php +++ b/tests/Unit/MultiplierTest.php @@ -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(