Skip to content

New ::submit method for fields #7130

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

Merged
merged 1 commit into from
Apr 10, 2025
Merged
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
14 changes: 14 additions & 0 deletions src/Form/Mixin/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ protected function setDefault(mixed $default = null): void
$this->default = $default;
}

/**
* Submits a new value for the field.
* Fields can overwrite this method to provide custom
* submit logic. This is useful if the field component
* sends data that needs to be processed before being
* stored.
*
* @since 5.0.0
*/
public function submit(mixed $value): static
{
return $this->fill($value);
Copy link
Member

@distantnative distantnative Apr 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understanding: E.g. with the picker fields, this would be the place where we could transform the big data array to simple list of (UU)IDs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly. We basically have two ways now:

::fill:
values come from storage, will be used for field components and might need to be transformed

::submit
values come from form submissions and will eventually go into storage and they might need to be transformed.

}

/**
* Returns the value of the field in a format to be used in forms
* (e.g. used as data for Panel Vue components)
Expand Down
11 changes: 11 additions & 0 deletions tests/Form/FieldClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,17 @@ public function testSiblings()
$this->assertSame('b', $field->siblings()->last()->name());
}

/**
* @covers ::submit
*/
public function testSubmit(): void
{
$field = new TestField();
$this->assertNull($field->value());
$field->submit('Test value');
$this->assertSame('Test value', $field->value());
}

/**
* @covers ::toStoredValue
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/Form/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,26 @@ public function testSiblings()
$this->assertSame('b', $field->formFields()->last()->name());
}

/**
* @covers ::submit
*/
public function testSubmit(): void
{
Field::$types = [
'test' => []
];

$field = new Field('test', [
'model' => new Page(['slug' => 'test']),
'value' => 'test'
]);

$this->assertSame('test', $field->value());

$field->submit('test2');
$this->assertSame('test2', $field->value());
}

/**
* @covers ::toArray
*/
Expand Down