FilamentF
Filamentβ€’9mo 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
Solution
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',
    ]);

It feels like there should be a better way of doing this πŸ€” Anyway, if it works...Β―\_(ツ)_/Β―
Was this page helpful?