php-laravel中的多文件上传验证

我有多个输入字段,每个字段有多个文件上传.

<?php for ($i = 0; $i < $total; $i++)
   {
?>
<div class="col-md-3 addedClass">
     <label>Vehicle Images</label>
     <input type="file" name="vehicle_image[{{$i}}][]" multiple="multiple">
     @if($errors->has('vehicle_image'))
          <span class="help-block">
              <strong>{{$errors->first('vehicle_image')}}</strong>
          </span>
     @endif
</div>
<?php } ?>

我在请求中有这样的文件:

"vehicle_image" => array:2 [▼
    0 => array:2 [▼
      0 => "citizenship.jpg"
      1 => "logo_vehicle.png"
    ]
    1 => array:2 [▼
      0 => "ae backend.jpg"
      1 => "logo_vehicle.png"
    ]
  ]

在这种情况下,我有2/2个文件的两个输入字段.当我尝试仅针对此类图像验证mime类型时:

$this->validate($request,[
           'vehicle_image' => 'mimes:jpeg,png,bmp,tiff |max:4096'
       ],$messages[
            // error messages 
      ]);

我有以下错误:

ReservationController.php 67行中的FatalThrowableError:无法使用[]进行读取

有人可以告诉我上述代码有什么问题吗?
建议表示赞赏.

解决方法:

尝试删除空白的$messages数组,并为请求中的输入调用函数all().

$rules = [
    'vehicle_image' => 'mimes:jpeg,png,bmp,tiff |max:4096'
];
$this->validate($request,$rules);

要显示默认错误消息,您将使用以下方法引发异常:

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
        return redirect('VIEWPATH.VIEWNAME')->withErrors($validator)->withInput();
    }

然后,要显示错误,您可以在刀片视图或模板中显示以下内容:

@if (count($errors) > 0)
  <div class="alert alert-danger alert-dismissable fade in">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <h4>
      <i class="icon fa fa-warning fa-fw" aria-hidden="true"></i>
      <strong>Error!</strong> See error messages...
    </h4>
    <ul>
      @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
      @endforeach
    </ul>
  </div>
@endif

要么

@if(session()->has('errors'))
    <div class="alert alert-danger fade in">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        <h4>Following errors occurred:</h4>
        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
上一篇:ASP.NET:如何在调用Page_ClientValidate之后“重置”验证


下一篇:为什么PHP DateTime和strtotime将“ a”和“ asdf”解析为有效日期?