mpyw/laravel-file-errors
最新稳定版本:v1.2.1
Composer 安装命令:
composer require mpyw/laravel-file-errors
包简介
A tiny extension that reports validation error details about uploaded files
README 文档
README
A tiny extension that reports validation error details about uploaded files
Requirements
- PHP:
^8.2 - Laravel:
^11.0 || ^12.0
Note
Older versions have outdated dependency requirements. If you cannot prepare the latest environment, please refer to past releases.
Installing
1. Install package
composer require mpyw/laravel-file-errors
2. Customize translation
Edit lang/{en,ja,...}/validation.php in your project.
Feel free to copy from lang.
<?php use Mpyw\LaravelFileErrors\UploadError as Err; return [ /* |-------------------------------------------------------------------------- | Validation Language Lines |-------------------------------------------------------------------------- | | The following language lines contain the default error messages used by | the validator class. Some of these rules have multiple versions such | as the size rules. Feel free to tweak each of these messages here. | */ /* ... */ // 'uploaded' => 'The :attribute failed to upload.', 'uploaded' => [ Err::TITLE_INI_SIZE => 'The :attribute exceeds the maximum filesize defined in the server.', Err::TITLE_FORM_SIZE => 'The :attribute exceeds the maximum filesize defined in the form.', Err::TITLE_PARTIAL => 'The :attribute was only partially uploaded.', Err::TITLE_NO_FILE => 'The :attribute was not uploaded.', Err::TITLE_CANT_WRITE => 'The :attribute could not be written on disk.', Err::TITLE_NO_TMP_DIR => 'The :attribute could not be uploaded; missing temporary directory.', Err::TITLE_EXTENSION => 'The :attribute upload was stopped by a PHP extension.', Err::TITLE_UNKNOWN => 'The :attribute could not be uploaded due to an unknown error.', ], /* ... */ ];
Basic Usage
Important
The default implementation is provided by ValidationServiceProvider, however, package discovery is not available.
Be careful that you MUST register it in config/app.php by yourself.
<?php return [ /* ... */ 'providers' => [ /* ... */ Mpyw\LaravelFileErrors\ValidationServiceProvider::class, /* ... */ ], ];
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class UserController extends Controller { public function update(Request $request) { $validator = Validator::make( $request->all(), [ 'name' => 'required|max:20', 'avatar' => 'required|image', ] ); // This may contain... // ['avatar' => ['The avatar exceeds the maximum filesize defined in the server.']] dump($validator->errors()->toArray()); } }
Advanced Usage
Tip
You can extend Validator with IncludesFileErrorDetails trait by yourself.
<?php namespace App\Providers; use App\Services\Validation\Validator; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Validator as Validation; class ValidationServiceProvider extends ServiceProvider { public function boot(): void { Validation::resolver(function (...$parameters) { return new Validator(...$parameters); }); } }
<?php namespace App\Services\Validation; use Illuminate\Validation\Validator as BaseValidator; use Mpyw\LaravelFileErrors\IncludesFileErrorDetails; class Validator extends BaseValidator { use IncludesFileErrorDetails; /* ... */ }
统计信息
- 总下载量: 3.65k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-11