gpt4 book ai didi

php - 拉拉维尔 :values placeholder not working?

转载 作者:行者123 更新时间:2023-12-01 19:34:11 25 4
gpt4 key购买 nike

我为扩展创建了一个自定义验证器:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com