| Package Data | |
|---|---|
| Maintainer Username: | Sukohi |
| Maintainer Contact: | capilano.sukohi@gmail.com (Sukohi) |
| Package Create Date: | 2016-05-12 |
| Package Last Update: | 2020-10-02 |
| Language: | PHP |
| License: | MIT |
| Last Refreshed: | 2025-11-05 15:03:22 |
| Package Statistics | |
|---|---|
| Total Downloads: | 50,287 |
| Monthly Downloads: | 179 |
| Daily Downloads: | 0 |
| Total Stars: | 8 |
| Total Watchers: | 1 |
| Total Forks: | 7 |
| Total Open Issues: | 2 |
A Laravel package to validate csv data. (This is for Laravel 5. For Laravel 4.2)
Execute composer command.
composer require sukohi/csv-validator:2.*
Register the service provider in app.php
'providers' => [
...Others...,
Maatwebsite\Excel\ExcelServiceProvider::class,
Sukohi\CsvValidator\CsvValidatorServiceProvider::class,
]
Also alias
'aliases' => [
...Others...,
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
'CsvValidator' => Sukohi\CsvValidator\Facades\CsvValidator::class,
]
$csv_path = 'test.csv';
$rules = [
0 => 'required',
1 => 'required|integer',
2 => 'required|min:4'
];
$csv_validator = CsvValidator::make($csv_path, $rules);
if($csv_validator->fails()) {
$errors = $csv_validator->getErrors();
} else {
$csv_data = $csv_validator->getData();
}
You can set keys instead of indexes like so.
$rules = [
'Product Title' => 'required',
'Product No.' => 'required',
'Position' => 'required'
];
In this case, the first row of the CSV need to have Product Title, Product No. and Position.
And This keys will be used as attribute names for error message.
If you set indexes for rules like so.
$rules = [
0 => 'required',
1 => 'required|integer',
2 => 'required|min:4'
];
You can set attribute names before calling fails() like this.
$csv_validator->setAttributeNames([
0 => 'Product Title',
1 => 'Product No.',
2 => 'Position'
]);
You can set a specific encoding as the 3rd argument.(Default: UTF-8)
CsvValidator::make($csv_path, $rules, 'SJIS-win');
You can get error messages after calling fails().
$errors = $csv_validator->getErrors();
foreach ($errors as $row_index => $error) {
foreach ($error as $col_index => $messages) {
echo 'Row '. $row_index .', Col '.$col_index .': '. implode(',', $messages) .'<br>';
}
}
You also can get CSV data after calling fails().
$csv_data = $csv_validator->getData();
In the case of the below, you must receive Exception.
e.g)
try {
$csv_validator = CsvValidator::make($csv_path, $rules, $encoding);
if($csv_validator->fails()) {
// Do something..
}
} catch (\Exception $e) {
echo $e->getMessage();
}
You can set delimiter, enclosure and line_ending through excel.php that Laravel Excel can publish by executing the following command.
php artisan vendor:publish
See here for the details.
This package is licensed under the LGPL License(following Laravel Excel).
Copyright 2016 Sukohi Kuhoh