在替换器中访问验证失败的值?

Gav*_*vin 7 validation laravel

在Laravel中使用自定义验证规则和替换程序时,我真的很难找到任何只允许您获得验证失败的值的文档.

例如,我创建了一个文件存在验证器:

Validator::extend('view_exists', function($field,$value,$parameters) 
{
    return View::exists($value);
});
Validator::replacer('view_exists', function($message, $attribute, $rule, $parameters)
{
    return str_replace(':filename', 'THE ENTERED VALUE', $message);
});
Run Code Online (Sandbox Code Playgroud)

现在,当我创建一个规则时:

$rules = array('filename' => 'required|view_exists');
$messages = array('filename.view_exists' => 'Filename \':filename\' does not exist');
Run Code Online (Sandbox Code Playgroud)

当我输入无效路径时,例如safsakjhdsafkljh,我希望它可以返回

Filename 'safsakjhdsafkljh' does not exist
Run Code Online (Sandbox Code Playgroud)

但是,replacer无法访问验证失败的值.我已经尝试输出传递给闭包的所有参数,包括$this并且它无处可见:(

在我使用Input::get(urgh)之前,我错过了一些完全明显的东西吗?

谢谢

加文

Ved*_*ant 6

使用 Laravel 5.4,您可以在替换回调中访问 Validator 实例,如下所示:

Validator::replacer('view_exists', function ($message, $attribute, $rule, $parameters, Validator $validator) {
    $value = array_get($validator->getData(), $attribute);

    return str_replace(':filename', $value, $message);
});
Run Code Online (Sandbox Code Playgroud)


Gav*_*vin 2

我怀疑我最初的想法是唯一的方法,但是如果有人能提出其他建议,我将不胜感激:

我的解决方案是:

Validator::extend('view_exists', function($field,$value,$parameters) 
{
    return View::exists($value);
});
Validator::replacer('view_exists', function($message, $attribute, $rule, $parameters)
{
    return str_replace(':filename', Input::get($attribute), $message);
});
Run Code Online (Sandbox Code Playgroud)

不是最伟大的,但是嘿,它有效......