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: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.<?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 这篇关于如何更改Laravel验证消息的最大文件大小以MB代替KB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-04 06:14
查看更多