Skip to content

Commit

Permalink
Form // don't submit any data in case the Form is disabled and don't …
Browse files Browse the repository at this point in the history
…allow re-submit of submitted forms. Allow inserting $inputData where the Form::name() is within that collection.
  • Loading branch information
Chrico committed Sep 19, 2023
1 parent 8c2cfb2 commit 79bdb40
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/Element/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Element/FormInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
61 changes: 61 additions & 0 deletions tests/phpunit/Unit/Element/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand Down

0 comments on commit 79bdb40

Please sign in to comment.