aymane-asserrar/livewire-excel-validator
Composer 安装命令:
composer require aymane-asserrar/livewire-excel-validator
包简介
Validate Excel uploads row-by-row. Returns a ValidationResult with per-cell errors, red-highlighted annotated file, hover comments, and a summary sheet.
README 文档
README
Validate Excel file uploads row-by-row against configurable column rules.
If errors are found, returns an annotated .xlsx file with red cell highlights and hover comments.
If clean, returns the parsed rows ready to insert into your database.
Installation
composer require aymane-asserrar/livewire-excel-validator
Requires PHP 8.1+ and Laravel 10/11/12 (auto-discovered via service provider).
Basic Usage
use AymaneAsserrar\ExcelValidator\ExcelValidator; $result = ExcelValidator::validate( file: $request->file('import'), // UploadedFile or a file path string rules: [ 'name' => ['required', 'string', 'max_length:100'], 'email' => ['required', 'email', 'unique'], 'age' => ['numeric', 'min:18', 'max:120'], 'role' => ['required', 'in:admin,editor,viewer'], 'status' => ['required', 'in:active,inactive'], ], headerRow: 1, // optional, defaults to 1 ); if ($result->hasErrors()) { // Save the annotated file path in the session and redirect session(['annotated_path' => $result->annotatedPath]); return back()->with('error', $result->errorCount() . ' error(s) found.'); } // No errors — use $result->rows to insert into the database foreach ($result->rows as $row) { User::create([ 'name' => $row['name'], 'email' => $row['email'], ]); }
Custom Column Labels
Add _label:My Label to any rule list to override how the column name appears in error messages:
'email' => ['required', 'email', 'unique', '_label:Email Address'],
Without _label, the column key is title-cased automatically (email_address → Email address).
Available Rules
| Rule | Description |
|---|---|
required |
Cell must not be empty |
string |
Must be non-numeric text |
numeric |
Must be a numeric value |
integer |
Must be a whole number |
email |
Must be a valid email address |
url |
Must be a valid URL |
date |
Must be a parseable date (or an Excel serial date) |
boolean |
Must be true/false, yes/no, or 1/0 |
min:{n} |
Numeric value must be ≥ n |
max:{n} |
Numeric value must be ≤ n |
min_length:{n} |
String length must be ≥ n characters |
max_length:{n} |
String length must be ≤ n characters |
in:{a,b,c} |
Value must be one of the listed options |
not_in:{a,b,c} |
Value must not be any of the listed options |
regex:{pattern} |
Value must match the regex (no delimiters — e.g. regex:^\d{4}$) |
unique |
Value must be unique within that column across all rows |
Rules are evaluated in order and stop at the first failure per cell.
Note (v2.0.0): Rows that are entirely blank are no longer skipped when any column carries a
requiredrule. Previously, a fully empty row would be silently ignored and therequirederror would never fire.
The ValidationResult Object
| Property / Method | Description |
|---|---|
$result->hasErrors() |
true if any row failed validation |
$result->errorCount() |
Total number of individual cell errors |
$result->errors |
Raw errors array: [rowIndex => [colKey => [messages]]] |
$result->flatErrors() |
Flat array: [['row' => n, 'column' => 'email', 'messages' => [...]]] |
$result->rows |
Parsed rows as array<int, array<string, string>>, keyed by lowercased header name |
$result->annotatedPath |
Absolute path to the annotated .xlsx file (null if no errors) |
$result->filename |
Original filename |
$result->download() |
Stream the annotated file as a download response (Laravel only) |
Serving the Annotated Error File
The annotated file is written to sys_get_temp_dir(). The recommended pattern is to store the path in the session and serve it via a dedicated route:
// In your controller / Livewire component session(['excel_errors_path' => $result->annotatedPath]); // In routes/web.php Route::get('/excel/download-errors', function () { $path = session('excel_errors_path'); abort_unless($path && file_exists($path), 404); session()->forget('excel_errors_path'); return response()->download($path, 'validation_errors.xlsx')->deleteFileAfterSend(true); })->name('excel.download-errors');
Or use the built-in helper directly in a controller:
return $result->download('my_errors.xlsx');
What the Annotated File Looks Like
- Red background + red text on every invalid cell
- Hover comment on each red cell listing the exact rule(s) that failed
- Amber header on every column that contains at least one error
Note: The "Validation Errors" summary sheet (Row / Column / Value / Error) was removed in v1.1.0. Use
$result->flatErrors()to access error details programmatically.
Non-Laravel Usage
The validator has no hard Laravel dependency. Pass a file path string instead of an UploadedFile:
$result = ExcelValidator::validate('/tmp/upload.xlsx', $rules);
ValidationResult::download() will throw a RuntimeException if called outside a Laravel context — use $result->annotatedPath directly and serve the file yourself.
License
MIT — Aymane Asserrar
aymane-asserrar/livewire-excel-validator 适用场景与选型建议
aymane-asserrar/livewire-excel-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「excel」 「xlsx」 「import」 「validation」 「laravel」 「phpspreadsheet」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aymane-asserrar/livewire-excel-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aymane-asserrar/livewire-excel-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aymane-asserrar/livewire-excel-validator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
Parse use statements for a reflection object
Tool for copying data from a production database to a dev database. Also useful for making backups of production databases.
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
A fork of konnco/filament-import with support of Laravel 11 since the default importer of Filament 3 is nonsense for basic use case.
Yii2 export extension
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-17