我为扩展创建了一个自定义验证器:
Validator::extend('extension', function ($attribute, $file, $extensions, $validator) {
$ext = strtolower(@$file->getClientOriginalExtension());
return in_array($ext, $extensions);
});
和自定义消息:
'extension' => 'The :attribute must be a file of type: :values.',
它似乎并没有取代该:values
零件。
我尝试过使用自定义替换也没有运气:
Validator::replacer('wtf', function ($message, $attribute, $rule, $parameters) {
return 'show me something!!!!!';
});
但这也不起作用。
缺少了什么?
Laravelvalues
默认不翻译占位符。您使用replacer
(docs)做正确的事。但是看起来您犯了一些错误。
ServiceProvider代码:
// in boot method define validator
Validator::extend('extension', function ($attribute, $file, $extensions, $validator) {
$ext = strtolower(@$file->getClientOriginalExtension());
return in_array($ext, $extensions);
});
// then - replacer with the same name
Validator::replacer('extension',
function ($message, $attribute, $rule, $extensions) {
return str_replace([':values'], [join(", ", $extensions)], $message);
});
在控制器中:
$validator = Validator::make($request->all(), [
'file' => 'required|extension:jpg,jpeg',
]);
在语言文件中:
'extension' => 'The :attribute must be a file of type: :values.',
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句