【问题标题】:Laravel :values placeholder not working?Laravel:值占位符不起作用?
【发布时间】:2017-06-29 13:05:16
【问题描述】:

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

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!!!!!';
});

但这也无济于事。

缺少什么?

【问题讨论】:

  • 您是否使用此消息进行了测试:The :attribute must be a file of type :values.?
  • 是的,它不起作用。另外我只是直接从mimes类型验证'mimes' => 'The :attribute must be a file of type: :values.',复制过来的

标签: php laravel laravel-5 laravel-validation


【解决方案1】:

Laravel 默认不翻译 values 占位符。您使用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.',

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-28
    • 2014-03-04
    • 2015-03-24
    • 1970-01-01
    • 2018-02-19
    • 2018-03-05
    • 2013-05-05
    • 2013-10-19
    相关资源
    最近更新 更多