How to change Laravel Validation message for max f

2019-05-01 23:37发布

Laravel comes with this validation message that shows file size in kilobytes:

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

I want to customize it in a way that it shows megabytes instead of kilobytes. So for the user it would look like:

"The document may not be greater than 10 megabytes."

How can I do that?

5条回答
神经病院院长
2楼-- · 2019-05-01 23:41

You can extend the validator to add your own rule and use the same logic without the conversion to kb. You can add a call to Validator::extend to your 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];
        });
    }
}

Then to add the error message you can just add it to your validation lang file.

See the section on custom validation rules in the manual

查看更多
做个烂人
3楼-- · 2019-05-01 23:42

We might be on different page, here is what I am trying to say. I hope this helps. Cheers!

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

public function messages()
{
    return [
        'file.max' => 'The document may not be greater than 10 megabytes'
    ];
}
查看更多
Summer. ? 凉城
4楼-- · 2019-05-01 23:46

File: 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);
    });
}

Don't forget to import proper class.


File: 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.',
...

Change config('upload.max_size') accordingly.

查看更多
虎瘦雄心在
5楼-- · 2019-05-01 23:48

Change the string in resources\lang\en\validation.php to

'file' => 'The :attribute may not be greater than 10 Megabytes.',

and define the $rule as

$rules = array(
   'file'=>'max:10000',
);
查看更多
一纸荒年 Trace。
6楼-- · 2019-05-02 00:04

This is how I would do validation using Request with custom validation messages in MB instead of KB:

  1. Create a Request file called StoreTrackRequest.php in App\HTTP\Requests folder with the following content

    <?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. Inside the controller make sure the validation is performed through StoreTrackRequest request:

    public function store($artist_id, StoreTrackRequest $request)
    {
         // Your controller code
    }
    
查看更多
登录 后发表回答