Laravel:价值占位符不起作用?

Rob*_*Rob 2 php laravel laravel-5 laravel-validation

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

Validator::extend('extension', function ($attribute, $file, $extensions, $validator) {
    $ext = strtolower(@$file->getClientOriginalExtension());

    return in_array($ext, $extensions);
});
Run Code Online (Sandbox Code Playgroud)

和自定义消息:

'extension' => 'The :attribute must be a file of type: :values.',
Run Code Online (Sandbox Code Playgroud)

它似乎没有取代:values零件.

我也尝试过使用自定义替换而没有运气:

Validator::replacer('wtf', function ($message, $attribute, $rule, $parameters) {
    return 'show me something!!!!!';
});
Run Code Online (Sandbox Code Playgroud)

但这也没有做任何事情.

少了什么东西?

shu*_*van 5

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);
});
Run Code Online (Sandbox Code Playgroud)

在控制器中:

$validator = Validator::make($request->all(), [
    'file' => 'required|extension:jpg,jpeg',
]);
Run Code Online (Sandbox Code Playgroud)

在语言文件中:

'extension' => 'The :attribute must be a file of type: :values.',
Run Code Online (Sandbox Code Playgroud)