'How to change Laravel Validation message for max file size in MB instead of KB?
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?
Solution 1:[1]
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'
];
}
Solution 2:[2]
This is how I would do validation using Request with custom validation messages in MB instead of KB:
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.' ]; } }Inside the controller make sure the validation is performed through StoreTrackRequest request:
public function store($artist_id, StoreTrackRequest $request) { // Your controller code }
Solution 3:[3]
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.
Solution 4:[4]
This function works
public function getSize()
{
$mb = 1000 * 1024;
if ($this->size > $mb)
return @round($this->size / $mb, 2) . ' MB';
return @round($this->size / 1000, 2) . ' KB';
}
Solution 5:[5]
In Laravel 8
To generate a new validation rule.
php artisan make:rule MaxSizeRule
Edit your rule as follows.
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class MaxSizeRule implements Rule
{
private $maxSizeMb;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($maxSizeMb = 8)
{
$this->maxSizeMb = $maxSizeMb;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$megabytes = $value->getSize() / 1024 / 1024;
return $megabytes < $this->maxSizeMb;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute may not be greater than ' . $this->maxSizeMb . ' MB.';
}
}
Use in your validation request as follow
return [
...
'banner_image' => ['required', 'file', 'mimes:jpeg,png,jpg,svg', new MaxSizeRule(10)],
...
];
Solution 6:[6]
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',
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pratik Kaje |
| Solution 2 | Mogsdad |
| Solution 3 | |
| Solution 4 | Silah Kosgei |
| Solution 5 | zarpio |
| Solution 6 |
