F
Filamentβ€’4mo ago
Steve_OH

No way to Mutate form data for created record using an action button on external resource

As the title suggests, I am attempting to create a record inside of another resource using a modal while passing values from the current record. The general use case is that I have a record open and would like to create a 'task' based on the current record. As such, I have a create action button, which triggers a form from an existing resource. The issue arises when I try to pass current form data into the modal. I found that mutateFormDataUsing, using, etc. seem to run after it's submitted, which is unhelpful. I did find afterFormFilled, which can be used to modify the table after default fields are entered, but there's not really any documentation on how to use it beyond simply stating that it can be used and my best efforts seem inconclusive. I have also messed around with $form->fill([]) (see below) as part of the form function, but may be doing it wrong as while the form still shows up, it does not pre-fill the values. My current efforts do not seem to yield correct results. I'd appreciate any help you can provide! My CreateAction button is called as follows:
\Filament\Actions\CreateAction::make('create')
->model(Task::class)
->icon('heroicon-o-plus')
->tooltip('Create Task')
->slideOver()
->form(function(Form $form) {
$form = auth()->user()->isAdmin ? TaskAdminResource::form($form) : TaskResource::form($form);
$form->fill([
'tickets' => 1, // Test value, does not work
]);

return $form;
})
->afterFormFilled(function () {
dd($this->form->getComponents());
})
\Filament\Actions\CreateAction::make('create')
->model(Task::class)
->icon('heroicon-o-plus')
->tooltip('Create Task')
->slideOver()
->form(function(Form $form) {
$form = auth()->user()->isAdmin ? TaskAdminResource::form($form) : TaskResource::form($form);
$form->fill([
'tickets' => 1, // Test value, does not work
]);

return $form;
})
->afterFormFilled(function () {
dd($this->form->getComponents());
})
My afterFormFilled dd function shows the form exists and I can see the components are present as expected, but I have no idea how to modify it from here. I've attempted to use Set, but this failed for some reason. Please help!
Solution:
What about the ->fillForm() method on the Action itself?
Jump to solution
7 Replies
Solution
Dennis Koch
Dennis Kochβ€’4mo ago
What about the ->fillForm() method on the Action itself?
Dennis Koch
Dennis Kochβ€’4mo ago
This is the part for the form definition. You can't fill anything from here.
->form(function(Form $form) {
$form = auth()->user()->isAdmin ? TaskAdminResource::form($form) : TaskResource::form($form);

$form->fill([
'tickets' => 1, // Test value, does not work
]);
->form(function(Form $form) {
$form = auth()->user()->isAdmin ? TaskAdminResource::form($form) : TaskResource::form($form);

$form->fill([
'tickets' => 1, // Test value, does not work
]);
Steve_OH
Steve_OHβ€’4mo ago
Literally exactly what I was looking for! Will try it out! Searched for hours for something like this and could not find it
Dennis Koch
Dennis Kochβ€’4mo ago
I entered Action::make()->fill and it was one of the first suggestions.
Steve_OH
Steve_OHβ€’4mo ago
I guess I was using the wrong prompts, I made the error of consulting with ChatGPT and spent an hour being given false information, which guided my documentation journey. Thanks for clarifying and I appreciate your dedication, filament is fantastic!
Dennis Koch
Dennis Kochβ€’4mo ago
The IDE should be your first choice before trusting a machine that often makes up random nonsense πŸ˜…
Steve_OH
Steve_OHβ€’4mo ago
Well, I can confirm this does work! :D. Thank you for your help!
\Filament\Actions\CreateAction::make('create')
->model(Task::class)
->icon('heroicon-o-plus')
->tooltip('Create Task')
->slideOver()
->modalHeading('Create a New Task')
->fillForm([
'users_id' => auth()->id(),
'visible' => auth()->id(),
'responsible' => auth()->id(),
])
->form(fn (Form $form) => auth()->user()->isAdmin ? TaskAdminResource::form($form) : TaskResource::form($form))
\Filament\Actions\CreateAction::make('create')
->model(Task::class)
->icon('heroicon-o-plus')
->tooltip('Create Task')
->slideOver()
->modalHeading('Create a New Task')
->fillForm([
'users_id' => auth()->id(),
'visible' => auth()->id(),
'responsible' => auth()->id(),
])
->form(fn (Form $form) => auth()->user()->isAdmin ? TaskAdminResource::form($form) : TaskResource::form($form))
Want results from more Discord servers?
Add your server