如何更改 Laravel 验证消息的最大文件大小(以 MB 而不是 KB 为单位)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34414278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Laravel Validation message for max file size in MB instead of KB?
提问by Glad To Help
Laravel comes with this validation message that shows file size in kilobytes:
Laravel 附带此验证消息,以千字节为单位显示文件大小:
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."
“该文件不得超过 10 兆字节。”
How can I do that?
我怎样才能做到这一点?
采纳答案by Ben Swinburne
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.
您可以扩展验证器以添加您自己的规则并使用相同的逻辑,而无需转换为 kb。您可以添加Validator::extend
对 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.
然后要添加错误消息,您只需将其添加到验证语言文件中即可。
回答by Pratik Kaje
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'
];
}
回答by Blesson Jose
This is how I would do validation using Request with custom validation messages in MB instead of KB:
这就是我使用 Request 进行验证的方式,其中自定义验证消息以 MB 而不是 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 }
在 App\HTTP\Requests 文件夹中创建一个名为 StoreTrackRequest.php 的请求文件,内容如下
<?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.' ]; } }
在控制器内部,确保通过 StoreTrackRequest 请求执行验证:
public function store($artist_id, StoreTrackRequest $request) { // Your controller code }
回答by Fahmi
File:app/Providers/AppServiceProvider.php
文件: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
文件: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.
相应地改变config('upload.max_size')
。
回答by Silah Kosgei
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';
}
回答by Amarnasan
Change the string in resources\lang\en\validation.php to
将 resources\lang\en\validation.php 中的字符串更改为
'file' => 'The :attribute may not be greater than 10 Megabytes.',
'file' => ': 属性不能大于 10 兆字节。',
and define the $rule
as
并将其定义$rule
为
$rules = array(
'file'=>'max:10000',
);