Skip to content

Commit

Permalink
fix csv file validation after import
Browse files Browse the repository at this point in the history
  • Loading branch information
ousid committed Sep 17, 2022
1 parent 5cd3761 commit f8cc424
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Concerns/HasCsvProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}
}
Expand Down
17 changes: 10 additions & 7 deletions src/Http/Livewire/CsvImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/CsvImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -70,7 +70,7 @@
])
->set('file', $file)
->assertSet('model', $model)
->assertHasErrors(['file_error']);
->assertHasErrors(['file']);
});

it('transfers columnsToMap into an associative array', function () {
Expand Down

0 comments on commit f8cc424

Please sign in to comment.