2017-02-11 55 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.',