覆盖Laravel验证消息 [英] Override laravel validation message
问题描述
我正在构建一个可以上传多个文件的表单,它的工作正常,但问题是我的验证消息,我的验证正在检查文件的mime类型.
I am building a form which uploads multiple files ,its working fine but the problem is my validation message ,my validation is checking the mime type of the file .
假设上传的文件名为文件名
验证消息: file.0必须是以下类型的文件:pdf,doc,docx,jpeg,jpg,png,bnp.
我想将其更改为文件名必须是以下类型的文件:pdf,doc,docx,jpeg,jpg,png,bnp.
我的表格:
{!! Form::open(['route' => ['file.store'], 'method'=> 'POST', 'id' => 'file_form','files' => 'true']) !!}
{!!Form::FILE('file[]', ['id' => 'file','multiple' =>true])!!}
<button type="submit" name="submit">save</button>
{!! Form::close() !!}
我的formRequest验证
my formRequest validation
foreach ($this->file as $key => $val)
{
$rules['file.' . $key] = 'mimes:pdf,doc,docx,jpeg,jpg,png,bnp|extension|max:10240'
}
return rules;
推荐答案
我使所有人都适合您的答案,我发现了另一种适合我项目的方式.我创建了名为 custom_mimes 的自定义验证,以基于mime类型验证文件类型,并添加了
I appropriate all of you for your answers,i have found another way that fits to my project. I have created custom validation called custom_mimes to validate file type based on mime type ,and adding custom replacer (:filename) to this validation.
\Validator::extend('custom_mimes', function ($attribute, $file, $parameters, $validator)
{
$validator->addReplacer('custom_mimes', function ($message, $attribute, $rule, $parameters) use ($file)
{
$values=implode(',',$parameters);
return str_replace([':filename',':values'], [$file->getClientOriginalName(),$values], $message);
});
$mime_type =$file->guessExtension();
return in_array($mime_type,$parameters);
});
和 validation.php
'custom_mimes' => 'The :filename must be a file of type: :values.',
所以我的键和值将是:
:文件名=>上传的文件名
:filename => uploaded filename
:values =>'jpg,png,doc,docx'
:values =>'jpg,png,doc,docx'
这篇关于覆盖Laravel验证消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!