2017-07-19 115 views
0

我目前在一個窗體中工作。Laravel多個文件上傳驗證

我有多個文件上傳驗證的問題。我只有一個表單允許多個文件上傳。

<input type="file" name="file[]" multiple="multiple"> 

這是我的驗證,

$this->validate($request, [ 
    'file' =>'required', 
    'file.*' => 'required|mimes:pdf,jpeg,png |max:4096', 
], 
$messages = [ 
      'mimes' => 'Only PDF, JPEG, PNG are allowed.' 
     ] 
); 

驗證完美的作品,但我不能夠在刀片文件顯示錯誤消息。

這是我的嘗試。

@if($errors->has('file')) 
    <span class="help-block"> 
     <strong>{{$errors->first('file')}}</strong> 
    </span> 
@endif 

這是爲了在沒有文件上傳時顯示錯誤。

想我已經上傳下列文件,

abc.jpg 
abc.html 
abc.pdf 

當默劇式的驗證拋出錯誤,我不能夠顯示錯誤消息。 在這裏,在這種情況下,由於在驗證索引1

失敗引發錯誤的$error->first(file.1)該指數可以根據上傳和$error->first(file.*)不工作以及文件的任何指標。

當我僅在窗體中添加無效文件後顯示所有錯誤時,出現這些錯誤。

Only PDF, JPEG, PNG are allowed. 
The type field is required. 
The number field is required. 
The expiry date field is required. 

任何人都有這個想法。任何幫助表示讚賞。

謝謝,

回答

0

這不是好辦法,但它是罰款我的情況。

我有驗證規則

'file' =>'required', 
'file.*' => 'required|mimes:pdf,jpeg,png |max:4096', 

而且,錯誤消息

'file.*' => 'Only PDF, JPEG, PNG are allowed.' 

因爲我只有一個文件上傳現場,我剛纔在所有的郵件,然後列表檢查這個錯誤訊息顯示如下。

<input type="file" name="file[]" multiple="multiple"> 
@foreach($errors->all() as $error) 
    @if($error=="Only PDF, JPEG, PNG are allowed.") 
      <span class="help-block"><strong>{{$error}}</strong></span> 
    @endif 
@endforeach 

感謝所有球員,

0

您可以使用檢查的圖像。

$errors->has('file.*') 
+0

我已經嘗試過這一點。這給錯誤**未定義偏移量:0 ** –

+0

請仔細閱讀,它一定會解決您的問題。 –

+0

https://laracasts.com/discuss/channels/general-discussion/array-validation-is-not-working/replies/125729 –

1

嘗試這樣的驗證

'file.*.mimes' => 'Only PDF, JPEG, PNG are allowed.', 
+0

驗證沒有問題。我遇到了錯誤信息 –

+0

一旦你使用了這個答案,只需要打印整個'$ errors'來查看這些消息,這樣你就會明白了。 – Hari

+0

當我打印所有錯誤時出現驗證錯誤,但是如何在那裏選擇該錯誤 –