如何在 Laravel 中接收数组验证的验证器数组的特定索引?

How can I receive a specific index of an validator array of array validation in Laravel?

我在 laravel 控制器中验证数组并使用 $errors->first('field_name')[= 接收消息24=] 或 $errors$errors->has('field_name) 。现在的问题是,我验证了一个名为 vaccine_certificate 的数组,但我无法收到错误消息。实际上,我在一次显示所有错误时收到消息。但我想用它的输入区域来展示它。我该如何解决?

$this->validate($request, [
        'name' => 'required',
        'employee_no' => 'required',
         'vaccine_certificate.*' => 'image|mimes:png,jpg|max:2048|dimensions:max_height=200,max_width=200',
        //'expertise' => 'required',
    ]);

输入字段看起来像

<div class="col-6 form-group">
                    <label class="control-label " for="vaccine_certificate"><h5 class="h6">Vaccine Certificate <small>(<2mb,png,jpg) </small> </h5></label>
                    <input class="form-control" type="file" name="vaccine_certificate[]" id="vaccine_certificate" multiple/>
                    @if ($errors->has('vaccine_certificate'))
                    <p id="employee_no_checker_message" class="text-danger p-2" onload="changeInputBorderColor('vaccine_certificate')"> {{ $errors->first('vaccine_certificate') }}</p>
                    @endif
                    
                </div>

您可以在表单错误包中使用和查看所有错误。

$errors->all()

但是,据我所知,vaccine_certificate中没有“required”,而你的规则是vaccine_certificate.*,这意味着它应该是一个数组。如果你像在名称字段中那样将其设置为 vaccine_certificate 并要求你可以看到错误消息。

如果需要使用数组,可以使用:

$errors->first('vaccine_certificate.*') 

因为它不是“必需的”,所以不会抛出错误,您也不会在 $errors 变量中看到它。