Return error from ->action
Hi,
Is it possible to return an error for example from a bulkaction->action? Almost like a validation message, my use case is I want to allow a user to select multiple table entries and extend them, but I first need to validate if they have enough balance, and alert them if not.
Thanks.
Solution:Jump to solution
Ah wait. Forgot that you probably don’t have a form. So then I would use a notification
9 Replies
So just do your check, send a notification and return before running the actual action?!
I think you could also use
->before(fn ($action) => $action->cancel())
I don't really like the idea of sending a notification and return before running the action, it feels somewhat of a dirty work around?
Isn't that exactly what you want? Only run the action if they got the balance?
I'd like it to work almost like a validation rule, if thats possible.
You could use a validation rule. Or a ViewField that shows the error, when you set a property on the action to true.
How do I use a validation rule within the action? Thanks
Or even the second option would work too.
Solution
Ah wait. Forgot that you probably don’t have a form. So then I would use a notification
You could create a Laravel rule class (read laravel documentation on this)
Add ->rule(YourRuleClass)
And then do the rest of the functionality in your ->action().
This way the rule/validation is executed before performing the action.
Note: you can do a validation in your rule and even display an error message in the form without customization the form.
Sadly I cannot see anyway to apply a rule to the action via ->rule
I was able to do this by creating a form first, and setting rules based off that! Thanks for the guidance.