Skip to content

Commit 79bdb40

Browse files
committed
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.
1 parent 8c2cfb2 commit 79bdb40

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

src/Element/Form.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,22 @@ public function withData(array $data = []): static
3939
/**
4040
* {@inheritDoc}
4141
*/
42-
public function submit(array $inputData = [])
42+
public function submit(array $inputData = []): void
4343
{
44+
$this->assertNotSubmitted(__METHOD__);
45+
46+
// By default, the form is valid.
4447
$this->isValid = true;
4548

49+
// A disabled form should not change its data upon submission.
50+
if ($this->isDisabled()) {
51+
$this->isSubmitted = true;
52+
}
53+
54+
// We also support as fallback that we send an array which contains
55+
// the Form::name() as entry-node.
56+
$inputData = $inputData[$this->name()] ?? $inputData;
57+
4658
foreach ($this->elements() as $name => $element) {
4759
// only validate elements which are not disabled.
4860
if ($element->isDisabled()) {

src/Element/FormInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ interface FormInterface
1414
*
1515
* @param array $inputData
1616
*/
17-
public function submit(array $inputData = []);
17+
public function submit(array $inputData = []): void;
1818

1919
/**
2020
* Pre-assign data (values) to all Elements when the Form is not submitted.

tests/phpunit/Unit/Element/FormTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ChriCo\Fields\Tests\Unit\Element;
44

5+
use ChriCo\Fields\Element\Element;
56
use ChriCo\Fields\Element\ElementInterface;
67
use ChriCo\Fields\Element\ErrorAwareInterface;
78
use ChriCo\Fields\Element\Form;
@@ -160,6 +161,29 @@ public function testSetData(): void
160161
);
161162
}
162163

164+
/**
165+
* @test
166+
*/
167+
public function testSubmitWithSubNode(): void
168+
{
169+
$expectedValue = 'my-value';
170+
171+
$element = new Element('my-element');
172+
173+
$testee = new Form('my-form');
174+
$testee->withElement($element);
175+
$testee->submit(
176+
[
177+
'my-form' => [
178+
'my-element' => $expectedValue,
179+
],
180+
]
181+
);
182+
183+
static::assertTrue($testee->isSubmitted());
184+
static::assertSame($expectedValue, $testee->element('my-element')->value());
185+
}
186+
163187
/**
164188
* @test
165189
*/
@@ -171,6 +195,43 @@ public function testSetDataAlreadySubmitted(): void
171195
$testee->withData(['foo' => 'bar']);
172196
}
173197

198+
/**
199+
* @test
200+
*/
201+
public function testSubmitMultipleTimes(): void
202+
{
203+
static::expectException(LogicException::class);
204+
$testee = new Form('');
205+
$testee->submit();
206+
$testee->submit();
207+
}
208+
209+
/**
210+
* @test
211+
*/
212+
public function testSubmitDisabledForm(): void
213+
{
214+
$expectedValue = 'foo';
215+
$element = new Element('my-element');
216+
$element->withValue($expectedValue);
217+
218+
$testee = new Form('');
219+
$testee->withElement($element);
220+
$testee->withAttribute('disabled', true);
221+
222+
static::assertTrue($testee->isDisabled());
223+
static::assertFalse($testee->isSubmitted());
224+
225+
static::assertTrue($element->isDisabled());
226+
static::assertFalse($element->isSubmitted());
227+
228+
$testee->submit(['my-element' => 'data']);
229+
230+
static::assertTrue($testee->isSubmitted());
231+
static::assertTrue($testee->isValid());
232+
static::assertSame($expectedValue, $element->value());
233+
}
234+
174235
/**
175236
* @test
176237
*/

0 commit comments

Comments
 (0)