sukohi/csv-validator
Composer 安装命令:
composer require sukohi/csv-validator
包简介
A Laravel package that allows you to validate csv data.
README 文档
README
A Laravel package that allows you to validate csv data.
This package is compatible with Laravel 5.7 -> 8.X
Installation
Run this command.
composer require sukohi/csv-validator:3.*
Basic usage
You can use a rule called Csv as usual validation.
And all the validation rules of Laravel is available for $csv_rules as follows.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Sukohi\CsvValidator\Rules\Csv;
class CsvValidatorController extends Controller
{
public function store(Request $request) {
$csv_rules = [
0 => 'required',
1 => 'integer',
2 => 'required|min:10'
];
$request->validate([
'users_csv' => [
new Csv($csv_rules) // <- here
]
]);
// Do something..
}
}
with Options
$csv_rules = [
0 => 'required',
1 => 'integer',
2 => 'required|min:10'
];
$options = [
'encoding' => 'sjsin-win',
'start_row' => 1, // <- Starting validation from row one, not zero. (*1)
'end_row' => 4, // <- Ending validation
'row_callback' => function($row_number, $row_data) {
return true; // `false` means skipping validation
}
];
$request->validate([
'csv_file' => [
new Csv($csv_rules, $options)
]
]);
(*1) For example, if you don't want to validate the header row, it means the first row, set 2 here.
Note: The former coding is also available.
new Csv($csv_rules, 'sjis-win')
Csv rules
You can skip column like this.
$csv_rules = [
0 => 'required',
3 => 'required|min:10'
];
$request->validate([
'csv_file' => [
new Csv($csv_rules)
]
]);
Retrieve
You can access csv data by adding _data after attribute of csv file as follows.
$csv_data = $request->csv_file_data; // when attribute is `csv_file`
Messages
Attributes of error messages are like this.
The A1 field is required.
The C1 field is required.
The C2 must be at least 10 characters.
The C3 must be at least 10 characters.
The C4 field is required.
Contributor
Thank you for your contributions!
License
This package is licensed under the MIT License.
Copyright 2020 Sukohi Kuhoh
sukohi/csv-validator 适用场景与选型建议
sukohi/csv-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50.8k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2016 年 05 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 sukohi/csv-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sukohi/csv-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 50.8k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-05-13