jeudi 4 août 2016

Error Changing Laravel Validation message for max file from KB to MB

I have a laravel 5 app. I'm trying to change the validation message from KB to MB. e.g The :attribute may not be greater than :max_mb megabytes.. Reading this answer: How to change Laravel Validation message for max file size in MB instead of KB?

I have my implementation as follows:

In app/Providers/AppServiceProvider.php, I have the code below:

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];
        });
    }

My custom rule is as follows:

'custom' => [
        'file' => [
            'max_mb' => 'The :attribute may not be greater than :max_mb megabytes.',
        ],
    ],

I get the error below when I try the upload:

BadMethodCallException in ServiceProvider.php line 234: 
Call to undefined method [requireParameterCount]

Methods requireParameterCount and getSize cannot be found. These methods are all implemented in illuminate/Validation/Validator.php. How can I resolve this issue?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire