2017-02-11 130 views
2

我创建了一个自定义的验证分机号:Laravel:值占位符不工作?

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

但是,这并不无论做任何事情。

什么是缺失?

+0

你有没有测试使用此消息:'的:属性必须是文件类型:values.'? – PaladiN

+0

是的,它不起作用。另外我直接从'mimes'类型验证''mimes'=>'The:属性必须是一个类型为::values的文件',' – Rob

回答

5

默认情况下,Laravel不会翻译values占位符。您通过使用replacerdocs)做了正确的事情。但看起来你犯了一些错误。

一个的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.',