Laravel数组验证 - 查找错误突出显示的数组键

时间:2017-11-02 01:48:53

标签: jquery laravel validation jquery.repeater

我有一个重复字段,允许用户使用https://github.com/DubFriend/jquery.repeater包一次上传多个文件。

我有一个表单请求验证,它有效,但在视图中我不知道如何只突出显示错误的字段。在下图中,只有第一行包含错误,但两行都突出显示。

enter image description here

在我的表单请求中,我有以下内容:

public function rules()
{
    return [
        ...
        'task_files.*.file_path' => 'required|max:10000',
        'task_files.*.title' => 'required|min:2|max:255',
        'task_files.*.file_description' => 'required|min:2|max:255',
        'task_files.*.file_type' => 'required|numeric',
    ];
}

在我看来,我有:

<div class="form-group{{ $errors->has('task_files.*.file_path') ? ' has-danger' : '' }}">
    {{ Form::file('file_path', ['class' => 'file_input'])}}
    @if ($errors->has('task_files.*.file_path'))
        <div class="form-control-feedback">
            {{ $errors->first('task_files.*.file_path') }}
        </div>
    @endif
</div>

看一下ViewErrorBag类,我看到失败字段的键存在,但是如何将它合并到我的视图中?

"task_files.0.file_description" => array:1 [▼
  0 => "The file description is required"
]

编辑:验证失败时,不会通过Blade模板中的循环创建字段。我使用$repeater.setList()使用Laravel old()辅助函数设置数据。

1 个答案:

答案 0 :(得分:0)

而不是检查是否

$errors->has('task_files.*.file_path')

在您的模板中,您渲染的每个文件输入,执行类似的检查,但使用文件输入的索引而不是星号:

$errors->has('task_files.0.file_path') //for first file input
$errors->has('task_files.1.file_path') //for second file input