0

I have a repeating field which allows users to upload multiple files at once, using the https://github.com/DubFriend/jquery.repeater package.

I have a form request for validation, which works, but in the view I don't know how to highlight only the fields where errors are present. In the image below, only the first row contains an error, but both rows are highlighted.

enter image description here

In my form request I have the following:

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',
    ];
}

In my view I have:

<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>

Having a look at the ViewErrorBag class, I see that the key of the failing field is present, but how can I incorporate this into my view?

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

Edit: When validation fails the fields are not created by a loop in the Blade template. I am using $repeater.setList() to set the data using the Laravel old() helper function.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Rob
  • 573
  • 4
  • 13

1 Answers1

0

Instead of checking if

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

in your template with every file input you render, do a similar check but use the file input's index instead of the asterisk:

$errors->has('task_files.0.file_path') //for first file input
$errors->has('task_files.1.file_path') //for second file input
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
  • Yes, that will work. The problem is that I'm setting the repeating fields from the old data with the $repeater.setList() method when the request fails, because I don't know how many fields there will be in advance. So I can't check the loop index in the Blade template. I think I need to pass the error data over to jQuery and somehow remove the error class where it isn't needed. – Rob Nov 02 '17 at 03:51