如何更改Laravel Validation消息的最大文件大小,以MB为单位而不是KB?

时间:2015-12-22 10:58:08

标签: laravel laravel-5.1 laravel-validation

Laravel附带此验证消息,显示文件大小,以千字节为单位:

file' => 'The :attribute may not be greater than :max kilobytes.',

我想以一种显示兆字节而不是千字节的方式自定义它。 因此,对于用户来说,它看起来像:

  

"文档不得超过10兆字节。"

我该怎么做?

6 个答案:

答案 0 :(得分:2)

我们可能在不同的页面,这是我想说的。我希望这有帮助。干杯!

public function rules()
{
    return [
        'file' => 'max:10240',
     ];
}

public function messages()
{
    return [
        'file.max' => 'The document may not be greater than 10 megabytes'
    ];
}

答案 1 :(得分:2)

您可以扩展验证器以添加您自己的规则并使用相同的逻辑而无需转换为kb。您可以将Validator::extend的呼叫添加到AppServiceProvider。

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('max_mb', function($attribute, $value, $parameters, $validator) {
            $this->requireParameterCount(1, $parameters, 'max_mb');

            if ($value instanceof UploadedFile && ! $value->isValid()) {
                return false;
            }

            // If call getSize()/1024/1024 on $value here it'll be numeric and not
            // get divided by 1024 once in the Validator::getSize() method.

            $megabytes = $value->getSize() / 1024 / 1024;

            return $this->getSize($attribute, $megabytes) <= $parameters[0];
        });
    }
}

然后添加错误消息,您只需将其添加到验证郎文件中即可。

See the section on custom validation rules in the manual

答案 2 :(得分:1)

这就是我如何使用带有MB的自定义验证消息而不是KB来验证:

  1. 在App \ HTTP \ Requests文件夹中创建一个名为StoreTrackRequest.php的请求文件,其中包含以下内容

    <?php
    
    namespace App\Http\Requests;
    
    use App\Http\Requests\Request;
    
    class StoreTrackRequest extends Request
    {
        /**
         * Determine if the user is authorized to make this request.
         *
         * @return bool
         */
        public function authorize()
        {
            return true;
        }
    
        /**
         * Get the validation rules that apply to the request.
         *
         * @return array
         */
        public function rules()
        {
            return [
                "name" => "required",
                "preview_file" => "required|max:10240",
                "download_file" => "required|max:10240"
            ];
        }
    
        /**
         * Get the error messages that should be displayed if validation fails.
         *
         * @return array
         */
        public function messages()
        {
            return [
                'preview_file.max' => 'The preview file may not be greater than 10 megabytes.',
                'download_file.max' => 'The download file may not be greater than 10 megabytes.'
            ];
        }
    }
    
  2. 在控制器内部,确保通过StoreTrackRequest请求执行验证:

    public function store($artist_id, StoreTrackRequest $request)
    {
         // Your controller code
    }
    

答案 3 :(得分:0)

将resources \ lang \ en \ validation.php中的字符串更改为

'file'=&gt; ':属性可能不会超过10兆字节。',

并将$rule定义为

$rules = array(
   'file'=>'max:10000',
);

答案 4 :(得分:0)

文件: app / Providers / AppServiceProvider.php

public function boot()
{
    Validator::extend('max_mb', function ($attribute, $value, $parameters, $validator) {

        if ($value instanceof UploadedFile && ! $value->isValid()) {
            return false;
        }

        // SplFileInfo::getSize returns filesize in bytes
        $size = $value->getSize() / 1024 / 1024;

        return $size <= $parameters[0];

    });

    Validator::replacer('max_mb', function ($message, $attribute, $rule, $parameters) {
        return str_replace(':' . $rule, $parameters[0], $message);
    });
}

不要忘记导入适当的课程。

文件: resources / lang / en / validation.php

'max_mb' => 'The :attribute may not be greater than :max_mb MB.',

'accepted'             => 'The :attribute must be accepted.',
'active_url'           => 'The :attribute is not a valid URL.',
...

相应地更改config('upload.max_size')

答案 5 :(得分:0)

此功能有效

public function getSize()
{
    $mb = 1000 * 1024;

    if ($this->size > $mb)
        return @round($this->size / $mb, 2) . ' MB';
    return @round($this->size / 1000, 2) . ' KB';
}