Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 260
Glad To Help Avatar asked Dec 22 '15 10:12

Glad To Help


People also ask

How do I increase post max size in laravel?

You don't have to increase it in Laravel, you have to increase it in PHP. In your php. ini there is a value that sets the maximum filesize PHP will accept. You have to talk to your hoster (if you don't manage your webserver yourself) that you need bigger filesizes.

What is the method used to configure validation rules in form request laravel?

Laravel Form Request class comes with two default methods auth() and rules() . You can perform any authorization logic in auth() method whether the current user is allowed to request or not. And in rules() method you can write all your validation rule.

Does the Laravel validator calculate size in KB or MB?

But the laravel validator for "max" calculates size in KB not in MB. laravel.com/docs/5.1/validation#rule-maxlaravel.com/docs/5.1/validation#rule-size

How to limit the size of an array in Laravel forms?

In your Form Request files or validate () method you can pass array with this parameter: The last part means that size should be not more than 20 MB (20000 kB). But that may be not enough, cause file restrictions exist not only on Laravel application level. There are two settings related to max size in php.ini file.

What is the max size of image in Laravel?

[ 'image' => 'required|mimes:jpeg,bmp,png|size:20000', ] The last part means that size should be not more than 20 MB (20000 kB). But that may be not enough, cause file restrictions exist not only on Laravel application level. There are two settings related to max size in php.ini file. Here they are with their default values:

How do I translate validation errors in Laravel?

Laravel's built-in validation rules each has an error message that is located in your application's lang/en/validation.php file. Within this file, you will find a translation entry for each validation rule. You are free to change or modify these messages based on the needs of your application.


1 Answers

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'
    ];
}
like image 92
Pratik Kaje Avatar answered Nov 15 '22 04:11

Pratik Kaje