FilamentF
Filament17mo ago
Tieme

2 "save" actions on CreateResource with "mutateFormDataBeforeCreate"

Hi All,

I'm aware that actions can be added to a Resource. I would like to add an additional "Save" button on the CreateResource page. My goal is to mutate one parameter (e.g., $data['status'] = InternalStatus::CONCEPT) only when this new button is pressed.

Does anyone have any ideas on how to achieve this?

Thanks in advance!
Solution
Here is the solution.

Create a Formfield, hide the label and add
hidden
as
extraAttributes


    Forms\Components\ToggleButtons::make('status')
        ->options(InternalProjectStatus::class)
        ->label(false)
        ->dehydrated(true)
        ->columnSpan('full')
        ->default(fn () => InternalProjectStatus::LOCKED)
        ->extraAttributes([
            'class' => 'hidden',
        ]),


For the action, set submit to
null
and update the
$livewire->data['status']
in action and than create the record.

This will set status to
Concept
in my case.

    protected function getCreateConceptFormAction(): Action
    {
        return Action::make('concept')
            ->label(Helpers::translate('Save as concept'))
            ->color(Color::Blue->getColor())
            ->submit(null)
            ->action(function ($livewire) {
                $livewire->data['status'] = InternalProjectStatus::CONCEPT;
                $this->create();
            })
            ->successNotification(
                Notifications\Notification::make()
                    ->success()
                    ->title('Order saves as concept'),
            )
            ->keyBindings(['mod+s']);
    }


If there is another way i would love to see it.
Because this works, but dont think this is the best way of doing this.
Was this page helpful?