diff --git a/src/Concerns/HasCsvProperties.php b/src/Concerns/HasCsvProperties.php index e603c80..178e0b9 100644 --- a/src/Concerns/HasCsvProperties.php +++ b/src/Concerns/HasCsvProperties.php @@ -54,8 +54,8 @@ public function handleCsvProperties(): array|MessageBag Log::warning($exception->getMessage()); return $this->addError( - 'file_error', - __('Your CSV file has error/errors, or is empty. Please check, and try again') + 'file', + __('The file has error/errors, Please check, and try again') ); } } diff --git a/src/Http/Livewire/CsvImporter.php b/src/Http/Livewire/CsvImporter.php index 4bff8e8..48040a2 100644 --- a/src/Http/Livewire/CsvImporter.php +++ b/src/Http/Livewire/CsvImporter.php @@ -114,17 +114,20 @@ protected function rules() protected function setCsvProperties() { + if (! $this->handleCsvProperties() instanceof MessageBag) { + return [ + $this->fileHeaders, + $this->fileRowCount + ] = $this->handleCsvProperties(); + } + $this->withValidator(function (Validator $validator) { $validator->after(function ($validator) { - if ($this->handleCsvProperties() instanceof MessageBag) { - $validator->errors()->merge( - $this->handleCsvProperties()->getMessages() - ); - } + $validator->errors()->merge( + $this->handleCsvProperties()->getMessages() + ); }); })->validate(); - - [$this->fileHeaders, $this->fileRowCount] = $this->handleCsvProperties(); } protected function importCsv() diff --git a/tests/CsvImporterTest.php b/tests/CsvImporterTest.php index a1f5a0a..d55a2f8 100644 --- a/tests/CsvImporterTest.php +++ b/tests/CsvImporterTest.php @@ -53,7 +53,7 @@ ]) ->set('file', $file) ->assertSet('model', $model) - ->assertHasErrors(['file_error']); + ->assertHasErrors(['file']); }); it('throws a validation error if the csv file has duplicate headers', function () { @@ -70,7 +70,7 @@ ]) ->set('file', $file) ->assertSet('model', $model) - ->assertHasErrors(['file_error']); + ->assertHasErrors(['file']); }); it('transfers columnsToMap into an associative array', function () {