我驗證 laravel 控制器中的陣列并使用$errors->first('field_name')或$errors或$errors->has('field_name)接收訊息。現在的問題是,我驗證了一個名為疫苗證書的陣列,但我無法收到錯誤訊息。實際上,當一次顯示所有錯誤時,我會收到訊息。但我想用它的輸入區域來展示它。我該如何解決?
$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>
uj5u.com熱心網友回復:
您可以使用并查看表單錯誤包中的所有錯誤。
$errors->all()
但是,據我所知,疫苗證書中沒有“必需”,您的規則是疫苗證書。*,這意味著它應該是一個陣列。如果您像在名稱欄位中所做的那樣將其設定為疫苗證書和必需,您可以看到錯誤訊息。
如果需要使用陣列,可以使用:
$errors->first('vaccine_certificate.*')
由于它不是“必需的”,因此它不會引發錯誤,并且您無法在 $errors 變數中看到它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/430332.html
