作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为扩展创建了一个自定义验证器:
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);
});
在 Controller 中:
$validator = Validator::make($request->all(), [
'file' => 'required|extension:jpg,jpeg',
]);
在语言文件中:
'extension' => 'The :attribute must be a file of type: :values.',
关于php - 拉拉维尔 :values placeholder not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42171982/
我是一名优秀的程序员,十分优秀!