From 79bdb40c1bbfdd749aaa912a27d414107e7f96d3 Mon Sep 17 00:00:00 2001 From: Christian Leucht Date: Tue, 19 Sep 2023 12:28:41 +0200 Subject: [PATCH] Form // don't submit any data in case the Form is disabled and don't allow re-submit of submitted forms. Allow inserting $inputData where the Form::name() is within that collection. --- src/Element/Form.php | 14 +++++- src/Element/FormInterface.php | 2 +- tests/phpunit/Unit/Element/FormTest.php | 61 +++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/Element/Form.php b/src/Element/Form.php index e01f72a..81257f8 100644 --- a/src/Element/Form.php +++ b/src/Element/Form.php @@ -39,10 +39,22 @@ public function withData(array $data = []): static /** * {@inheritDoc} */ - public function submit(array $inputData = []) + public function submit(array $inputData = []): void { + $this->assertNotSubmitted(__METHOD__); + + // By default, the form is valid. $this->isValid = true; + // A disabled form should not change its data upon submission. + if ($this->isDisabled()) { + $this->isSubmitted = true; + } + + // We also support as fallback that we send an array which contains + // the Form::name() as entry-node. + $inputData = $inputData[$this->name()] ?? $inputData; + foreach ($this->elements() as $name => $element) { // only validate elements which are not disabled. if ($element->isDisabled()) { diff --git a/src/Element/FormInterface.php b/src/Element/FormInterface.php index 415c2a5..5e8b814 100644 --- a/src/Element/FormInterface.php +++ b/src/Element/FormInterface.php @@ -14,7 +14,7 @@ interface FormInterface * * @param array $inputData */ - public function submit(array $inputData = []); + public function submit(array $inputData = []): void; /** * Pre-assign data (values) to all Elements when the Form is not submitted. diff --git a/tests/phpunit/Unit/Element/FormTest.php b/tests/phpunit/Unit/Element/FormTest.php index 0c058e7..296a08d 100644 --- a/tests/phpunit/Unit/Element/FormTest.php +++ b/tests/phpunit/Unit/Element/FormTest.php @@ -2,6 +2,7 @@ namespace ChriCo\Fields\Tests\Unit\Element; +use ChriCo\Fields\Element\Element; use ChriCo\Fields\Element\ElementInterface; use ChriCo\Fields\Element\ErrorAwareInterface; use ChriCo\Fields\Element\Form; @@ -160,6 +161,29 @@ public function testSetData(): void ); } + /** + * @test + */ + public function testSubmitWithSubNode(): void + { + $expectedValue = 'my-value'; + + $element = new Element('my-element'); + + $testee = new Form('my-form'); + $testee->withElement($element); + $testee->submit( + [ + 'my-form' => [ + 'my-element' => $expectedValue, + ], + ] + ); + + static::assertTrue($testee->isSubmitted()); + static::assertSame($expectedValue, $testee->element('my-element')->value()); + } + /** * @test */ @@ -171,6 +195,43 @@ public function testSetDataAlreadySubmitted(): void $testee->withData(['foo' => 'bar']); } + /** + * @test + */ + public function testSubmitMultipleTimes(): void + { + static::expectException(LogicException::class); + $testee = new Form(''); + $testee->submit(); + $testee->submit(); + } + + /** + * @test + */ + public function testSubmitDisabledForm(): void + { + $expectedValue = 'foo'; + $element = new Element('my-element'); + $element->withValue($expectedValue); + + $testee = new Form(''); + $testee->withElement($element); + $testee->withAttribute('disabled', true); + + static::assertTrue($testee->isDisabled()); + static::assertFalse($testee->isSubmitted()); + + static::assertTrue($element->isDisabled()); + static::assertFalse($element->isSubmitted()); + + $testee->submit(['my-element' => 'data']); + + static::assertTrue($testee->isSubmitted()); + static::assertTrue($testee->isValid()); + static::assertSame($expectedValue, $element->value()); + } + /** * @test */