Validate import before inserting records.

Hello. I'm playing around with import actions to see if it suits my needs. Do yo know if its possible to validate the CSV before inserting data in the database?. I need to be able to cancel de import if data is invalid. I read on docs that the import process collects all failed rows but does that mean that valid data get imported? This is a problem to me as partial import is hard to handle and will prefer to avoid import at all if data fails validation. In order to fix it and attempt the import again. Thanks.
2 Replies
Jean Roumeau
Jean RoumeauOP7mo ago
According to docs (https://filamentphp.com/docs/3.x/actions/prebuilt-actions/import#customizing-import-validation-messages) it also says that "the import system will automatically validate the CSV file before it is imported". But ot doesn't seem to work like this.
Tetracyclic
Tetracyclic7mo ago
https://github.com/filamentphp/filament/blob/2f5ac6078d522672a437b56d573a78ecf869a67a/packages/actions/src/Imports/Importer.php#L142 The Importer uses the validation rules from the column to validate it, but it processes each row separately, and it chunks the CSV across multiple jobs. You can pass in a custom import job that extends ImportCsv. You could alter it to first run through the records and validate them and only then if there are no failures, run it through the standard importer. But you'd need to ensure that all of the records are processed in a single chunk and not sent to multiple jobs. https://filamentphp.com/docs/3.x/actions/prebuilt-actions/import#customizing-the-import-job https://github.com/filamentphp/filament/blob/2f5ac6078d522672a437b56d573a78ecf869a67a/packages/actions/src/Imports/Jobs/ImportCsv.php#L76
Want results from more Discord servers?
Add your server