Skip to content

Commit b0ec54d

Browse files
committed
mutateFormDataBeforeSave for Edit Action
1 parent 11e0ae6 commit b0ec54d

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/Actions/EditAction.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class EditAction extends Action
1414

1515
protected ?Closure $mutateRecordDataUsing = null;
1616

17+
protected ?Closure $mutateFormDataBeforeSaveUsing = null;
18+
1719
public static function getDefaultName(): ?string
1820
{
1921
return 'edit';
@@ -45,9 +47,12 @@ protected function setUp(): void
4547

4648
$this->action(function (): void {
4749
$this->process(function (array $data, Model $record) {
50+
if ($this->mutateFormDataBeforeSaveUsing) {
51+
$data = $this->evaluate($this->mutateFormDataBeforeSaveUsing, ['data' => $data]);
52+
}
4853
$record->update($data);
4954
});
50-
55+
5156
$this->success();
5257
});
5358
}
@@ -58,4 +63,16 @@ public function mutateRecordDataUsing(?Closure $callback): static
5863

5964
return $this;
6065
}
66+
67+
public function mutateFormDataBeforeSaveUsing(?Closure $callback): static
68+
{
69+
$this->mutateFormDataBeforeSaveUsing = $callback;
70+
71+
return $this;
72+
}
73+
74+
public function getMutateFormDataBeforeSave(): ?Closure
75+
{
76+
return $this->mutateFormDataBeforeSaveUsing;
77+
}
6178
}

src/Concern/TreeRecords/Translatable.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ protected function afterConfiguredEditAction(Actions\EditAction $action): Action
7575
});
7676

7777
if (method_exists($action, 'using')) {
78-
$action->using(function (array $data, Model $record) {
78+
$action->using(function (array $data, Model $record) use ($action) {
79+
80+
$data = $action->evaluate($action->getMutateFormDataBeforeSave(), ['data' => $data]);
81+
7982
$record->fill($data);
8083
if (method_exists($record, 'setTranslation') &&
8184
method_exists($record, 'getTranslatableAttributes')

src/Pages/TreePage.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ protected function configureEditAction(Actions\EditAction $action): Actions\Edit
144144

145145
$action->model($this->getModel());
146146

147+
$action->mutateFormDataBeforeSaveUsing(fn (array $data) => $this->mutateFormDataBeforeSave($data));
148+
147149
$this->afterConfiguredEditAction($action);
148150

149151
return $action;
@@ -224,6 +226,11 @@ protected function getActions(): array
224226
);
225227
}
226228

229+
protected function mutateFormDataBeforeSave(array $data): array
230+
{
231+
return $data;
232+
}
233+
227234
protected function callHook(string $hook): void
228235
{
229236
if (! method_exists($this, $hook)) {

0 commit comments

Comments
 (0)