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));
}
};
}
])
])
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!
5 Replies
toeknee
toeknee2y ago
Did you look at the filament Import Plugin? https://filamentphp.com/plugins/import https://github.com/konnco/filament-import#validation You can then add validation to the field it's self too.
Filament
Import by Franky So - Plugins - Filament
import without the need to do templates. all you have to do is drag and drop and match the fields and columns of your file, and let the magic happens!
GitHub
GitHub - konnco/filament-import: why import must use a template whe...
why import must use a template when you can import all files dynamically? - GitHub - konnco/filament-import: why import must use a template when you can import all files dynamically?
Laurens
LaurensOP2y ago
I've seen the package, but that one only does (mass) inserts right? I'll need to do custom logic of how the imported rows are processed, which could be done inside the Import class from maatwebsite/excel using the ToConcern collection.
Laurens
LaurensOP2y ago
It works, but it closes the modal and throws the errors as a notification as well, doesn't seem ideal.
toeknee
toeknee2y ago
GitHub
GitHub - konnco/filament-import: why import must use a template whe...
why import must use a template when you can import all files dynamically? - GitHub - konnco/filament-import: why import must use a template when you can import all files dynamically?
Laurens
LaurensOP2y ago
Yeah, but validation works less good than my custom soluton above - the modal is closed (we keep it open) and validation goes into notifications
Want results from more Discord servers?
Add your server