Excel upload validation in Resource Action

I'm looking to validate an Excel import (maatwebsite/excel:^3.1) inside a resource action.

Currently I'm using a closure rule, where I'm collecting all errors from the Excel import:

Action::make('Import')
                ->color('secondary')
                ->action(function ($data) {
                    ...
                })
                ->form([
                    Forms\Components\FileUpload::make('excel')
                        ->disk('local')
                        ->directory('filament-import')
                        ->rules([
                            function () {
                                return static function (string $attribute, $value, Closure $fail) {
                                    try {
                                        Excel::import(new UsersImport, $value);
                                    } catch (ValidationException $e) {
                                        $errors = [];

                                        foreach ($e->failures() as $failure) {
                                            foreach ($failure->errors() as $error) {
                                                $errors[] = 'Row ' . $failure->row() . ': ' . $error;
                                            }
                                        }

                                        $fail(implode(PHP_EOL, $errors));
                                    }
                                };
                            }
                        ])
                ])


The above works, but I cannot find a way to print every error on a line. Is there a way to do this?

I've also tried validating in the action callback... but I can only find how to send notifications from there, and cannot seem to insert validation errors back to the form there.

(when it works I'll add a parameter to the import construction to ignore the collection and only run the validation)

Thanks in advance!
Was this page helpful?