F
Filament14mo ago
Mdk

Custom Action that calls "save" doesn't actually save the model

So I'm trying to put a save button elsewhere in an Edit page
Grid::make('card_payment')
->visible(fn($get) => $get('payment_method') === 'card')
->schema([
TextInput::make('balance_used')
->label(__uf('use balance'))
->hint(__uf('balance available').': '.money(auth()->user()->balance, env('CASHIER_CURRENCY')))
->numeric()
->minValue(0)
->maxValue(auth()->user()->balance)
->live(debounce:500)
//->afterStateUpdated(fn($state) => dump($state))
->prefix('€'),
Actions::make([
Actions\Action::make('paybycard')
->label(fn($record, $get) =>
__uf('proceed to payment').' - '.money($record->order_total + $record->shipping_cost - ($get('balance_to_use')?:0), env('CASHIER_CURRENCY')))
->submit('save')
])->fullWidth()
])
Grid::make('card_payment')
->visible(fn($get) => $get('payment_method') === 'card')
->schema([
TextInput::make('balance_used')
->label(__uf('use balance'))
->hint(__uf('balance available').': '.money(auth()->user()->balance, env('CASHIER_CURRENCY')))
->numeric()
->minValue(0)
->maxValue(auth()->user()->balance)
->live(debounce:500)
//->afterStateUpdated(fn($state) => dump($state))
->prefix('€'),
Actions::make([
Actions\Action::make('paybycard')
->label(fn($record, $get) =>
__uf('proceed to payment').' - '.money($record->order_total + $record->shipping_cost - ($get('balance_to_use')?:0), env('CASHIER_CURRENCY')))
->submit('save')
])->fullWidth()
])
Both payment_method and balance_used are properties of the model that is being edited I tried with both submit('save') as well as action('save'), they both trigger the validation and display the success notification, but the model is not updated and neither protected function beforeSave() or protected function afterSave() are called.. what am I doing wrong? My plan was to save the order and then redirect to the payment page in the afterSave method, I would've used a custom action but I couldn't manage to trigger the validation with that
3 Replies
Mdk
MdkOP14mo ago
(that commented afterStateUpdated was to check if it was actually updated, and yes, it triggered, but still no validation)
Vp
Vp14mo ago
I think you need to use ->action() inside Action like below (just copy paste from docs)
Action::make('sendEmail')
->form([
TextInput::make('subject')->required(),
RichEditor::make('body')->required(),
])
->action(function (array $data) {
Mail::to($this->client)
->send(new GenericEmail(
subject: $data['subject'],
body: $data['body'],
));
})
Action::make('sendEmail')
->form([
TextInput::make('subject')->required(),
RichEditor::make('body')->required(),
])
->action(function (array $data) {
Mail::to($this->client)
->send(new GenericEmail(
subject: $data['subject'],
body: $data['body'],
));
})
Read more here https://filamentphp.com/docs/3.x/actions/overview#what-is-an-action
Mdk
MdkOP14mo ago
i'm gonna give that a try, but why am I getting the save notification if the model isn't actually saved? ah, my custom action didn't run the validations, but I could try doing that manually
Want results from more Discord servers?
Add your server