Laravel :values 占位符不起作用?
Laravel :values placeholder not working?
我已经为扩展创建了一个自定义验证器:
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!!!!!';
});
但这也没有任何作用。
缺少什么?
Laravel 默认不翻译 values
占位符。您使用 replacer
(docs) 做对了。但是看起来你犯了一些错误。
服务提供商代码:
// 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.',
我已经为扩展创建了一个自定义验证器:
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!!!!!';
});
但这也没有任何作用。
缺少什么?
Laravel 默认不翻译 values
占位符。您使用 replacer
(docs) 做对了。但是看起来你犯了一些错误。
服务提供商代码:
// 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.',