Laravel 附带此验证消息,以千字节为单位显示文件大小:
file' => 'The :attribute may not be greater than :max kilobytes.',
我想以显示兆字节而不是千字节的方式对其进行自定义。 所以对于用户来说它看起来像:
“文档不得大于 10 兆字节。”
我该怎么做?
我们可能在不同的页面上,这就是我想说的。我希望这有帮助。干杯!
public function rules()
{
return [
'file' => 'max:10240',
];
}
public function messages()
{
return [
'file.max' => 'The document may not be greater than 10 megabytes'
];
}
文件: 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);
});
}
不要忘记导入正确的类。
文件: 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.',
...
相应地改变
config('upload.max_size')
。
您可以创建自己的规则,并使用与现有规则相同的逻辑,但无需转换为 KB。
为此,请在您的
Validator::extend
文件中添加对 AppServiceProvider.php
方法的调用,例如:
<?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];
});
}
}
然后要更改错误消息,您只需编辑验证语言文件即可。
另见手册中关于自定义验证规则的部分
这个功能有效
public function getSize()
{
$kb = 1024;
$mb = $kb * 1024;
if ($this->size > $mb)
return @round($this->size / $mb, 2) . ' MB';
return @round($this->size / $kb, 2) . ' KB';
}
这就是我将如何使用带有自定义验证消息的 Request 进行验证,而不是 KB:
在 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
}
在Laravel 8
生成新的验证规则。
php artisan make:rule MaxSizeRule
按如下方式编辑您的规则。
<?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.';
}
}
在您的验证请求中使用如下
return [
...
'banner_image' => ['required', 'file', 'mimes:jpeg,png,jpg,svg', new MaxSizeRule(10)],
...
];
将resources\lang n alidation.php中的字符串改为
'file' => 'The :attribute may not be greater than 10 Megabytes.',
并将
$rule
定义为
$rules = array(
'file'=>'max:10000',
);