mpyw/laravel-file-errors

一个报告上传文件验证错误详细信息的微小扩展

v1.1.0 2023-03-13 06:17 UTC

This package is auto-updated.

Last update: 2024-09-21 19:37:18 UTC


README

一个报告上传文件验证错误详细信息的微小扩展

要求

  • PHP: ^8.0
  • Laravel: ^9.0 || ^10.0

安装

1. 安装包

composer require mpyw/laravel-file-errors

2. 自定义翻译

编辑项目中 lang/{en,ja,...}/validation.php
请随意从 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.',
    ],

    /* ... */
];

基本用法

重要

默认实现由 ValidationServiceProvider 提供,然而,包发现不可用。请小心,您必须自己将其注册在 config/app.php 中。

<?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());
    }
}

高级用法

技巧

您可以通过自己扩展 Validator 来使用 IncludesFileErrorDetails 特性。

<?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;

    /* ... */
}