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?
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.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
We might be on different page, here is what I am trying to say. I hope this helps. Cheers!
File: app/Providers/AppServiceProvider.php
Don't forget to import proper class.
File: resources/lang/en/validation.php
Change
config('upload.max_size')
accordingly.Change the string in resources\lang\en\validation.php to
'file' => 'The :attribute may not be greater than 10 Megabytes.',
and define the
$rule
asThis 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
Inside the controller make sure the validation is performed through StoreTrackRequest request: