如何使用相同的验证规则验证数组中收到的整个请求?

How can I validate the entire request received within an array with the same validation rule?

我正在尝试验证通过 $ request () -> all () 收到的所有请求。它们必须是全部,因为收到的金额可能会有所不同,因为我在 foreach 中有可以改变其金额的输入。

所以我需要使用验证规则 'string' 和 'required'

验证所有内容
@foreach ($questions as $index => $question)
<div class="col-md-12">
  <div class="form-group row">
     <div class="col-md-4 mt-2">
         <label for="form-control-label" class="d-flex justify-content-end"> 
           <b{{$question>description }}</b></label>
     </div>
            <div class="col-md-3">
                <input type="text" name="sectionTime{{ $index }}" class="form-control">
                <div class="text-center"> 
                    {!! $errors->first('sectionTime'.$index, '<strong class="text-danger">:message</strong>') !!}
                </div>
            </div>
      </div>
</div>
@endforeach

$req = $request->all();
$validator = Validator::make($request->all(), [
             $req => ['required', 'string']
        ]);

我试过这样做,但没有用。 有什么办法可以做到吗?

你最好将输入类型设置为数组(也使用索引)

<input type="text" name="sectionTime[{{ $index }}]" class="form-control">

然后验证将使用 nested array input

的格式
$validator = Validator::make($request->all(), [
    'sectionTime.*' => 'required|string'
]);

之后,您就可以访问这些输入了

$sectionTime = $request->input('sectionTime');
//section one
$sectionTime['index_one']; // or $sectionTime[1]... use the same indexes as the on you set in the html