F
Filamentβ€’6d ago
frame

Prevent form from closing and show error on action form submit

Is it possible to prevent the form from closing and display an error message when an action form submit fails? I want to run a DB transaction in ->after (or another similar lifecycle method), and if it fails I don't want the form to submit, but instead show what went wrong.
Tables\Actions\Action::make('upload csv')
->form(function (Form $form) {
return $form->schema([
FileUpload::make('csv')
]);
})
->after(function ($data, Component $livewire) use ($group) {
$imports = Csv::readCsv($data['csv']->getPathName());
$members = AddToGroup::addMembers($group, $imports); // throws
Tables\Actions\Action::make('upload csv')
->form(function (Form $form) {
return $form->schema([
FileUpload::make('csv')
]);
})
->after(function ($data, Component $livewire) use ($group) {
$imports = Csv::readCsv($data['csv']->getPathName());
$members = AddToGroup::addMembers($group, $imports); // throws
Solution:
Hmm I've been trying things out (throwing πŸ’© in the wall pretty much haha). Not sure if this is better or worse? ```php ->action(function (Form $form, Action $action, $livewire) { throw ValidationException::withMessages([ $form->getFlatFields()['foo']->getStatePath() => 'Something went wrong',...
Jump to solution
9 Replies
ChesterS
ChesterSβ€’5d ago
Not sure if that's what you want, but you can do something like
->after(function ($data, Action $action) {
try {
...
} catch(Throwable ) {
Notification::make()->body('Something went wrong...')->send();
$action->halt();
}
});
->after(function ($data, Action $action) {
try {
...
} catch(Throwable ) {
Notification::make()->body('Something went wrong...')->send();
$action->halt();
}
});
This will prevent the modal from closing. The notification part is optional You can do the same thing in the ->action() part AFAIK
frame
frameOPβ€’5d ago
Hey, awesome πŸ‘€ $action->halt(); works great. Now I just need to figure out how to display an error in the form itself
Illizian
Illizianβ€’5d ago
I'm very curious to follow along with this. I've just thrown a ValidationException from an action but the namespace didn't fill me with confidence and felt flimsy. It was something like mountedActionData.0.field
frame
frameOPβ€’5d ago
Sounds interesting πŸ‘€ . I'm thinking about having a conditional Placeholder or something as a pseudo validation error message, but not sure how to set the content yet.
Illizian
Illizianβ€’5d ago
ValidationException::withMessages([
// @TODO: Find a neater way of throwing this, I do not like the namespace below!!
"mountedTableActionsData.0.{$name}" => __('Error Message'),
]));
ValidationException::withMessages([
// @TODO: Find a neater way of throwing this, I do not like the namespace below!!
"mountedTableActionsData.0.{$name}" => __('Error Message'),
]));
complete with my shame // @TODO haha
frame
frameOPβ€’5d ago
Thanks, that does work way better. πŸ‘ I'll leave the todo for future reference Interestingly the zero is $mountedTableActionIndex but this the second action in the table I'm working on and 0 still works πŸ‘€
Solution
ChesterS
ChesterSβ€’5d ago
Hmm I've been trying things out (throwing πŸ’© in the wall pretty much haha). Not sure if this is better or worse?
->action(function (Form $form, Action $action, $livewire) {
throw ValidationException::withMessages([
$form->getFlatFields()['foo']->getStatePath() => 'Something went wrong',
]);
->action(function (Form $form, Action $action, $livewire) {
throw ValidationException::withMessages([
$form->getFlatFields()['foo']->getStatePath() => 'Something went wrong',
]);
It feels like there should be a better way of doing this πŸ€” Anyway, if it works...Β―\_(ツ)_/Β―
Illizian
Illizianβ€’5d ago
oooooh!!! I definetly prefer that!
frame
frameOPβ€’5d ago
πŸ‘€ That's nice, I'll use ->action() instead

Did you find this page helpful?