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.
Solution:Jump to 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',...
9 Replies
Not sure if that's what you want, but you can do something like
This will prevent the modal from closing. The notification part is optional
You can do the same thing in the
->action()
part AFAIKHey, awesome π
$action->halt();
works great. Now I just need to figure out how to display an error in the form itselfI'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
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.
complete with my shame
// @TODO
hahaThanks, 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
Hmm I've been trying things out (throwing π© in the wall pretty much haha). Not sure if this is better or worse?
It feels like there should be a better way of doing this π€ Anyway, if it works...Β―\_(γ)_/Β―
oooooh!!! I definetly prefer that!
π That's nice, I'll use
->action()
instead