2017-02-25 90 views
0

這似乎並不適用於我的代碼。我試圖使用JQuery表單驗證來驗證表單,其中一個字段有一個圖像數組(或文件輸入列表)。我擺弄了很多不同的選擇,但沒有掩蓋。JQuery驗證規則不適用於輸入字段中的文件陣列

這是我想做的事: 假設我有一種形式,它有一個輸入字段,需要多張圖片:

{!! Form::file('images[]', array('multiple'=>true,'id'=>'images', 'class' => 'form-control btn btn-default')) !!} 

我試過如下:

$("#myform").validate({ 
    rules: { 
images[]: { 
    required: true, 
    accept: "image/*" 
} 
    }, 
messages: 
      { 
       images[]: "Title field cannot be blank!" 

      } 
}); 

回答

0

在PHP ,提交的值如field[]會導致名爲$ field的變量包含附加項目。

$field = array(); 
$field[] = "A"; 
$field[] = "B" 

相同

$field = array(); 
array_push($field, "A"); 
array_push($field, "B"); 

在JavaScript中,然而,該語法是未知的。你要麼必須包裝的陣列周圍的field[]項目的JSON-對象或刪除[]

$("#myform").validate({ 
    rules: { 
    field: { 
     required: true, 
     accept: "image/*" 
    } 
    }, 
    messages: 
    { 
    field: "Title field cannot be blank!" 
    } 
}); 

在你的情況,我不明白爲什麼你打電話給你的變量field。我想你想叫它images

$("#myform").validate({ 
    rules: { 
    images: { 
     required: true, 
     accept: "image/*" 
    } 
    }, 
    messages: 
    { 
    images: "Title field cannot be blank!" 
    } 
}); 
+0

我的ID刪除[]做一個像你,它不會引發任何錯誤消息 – Jose

+0

你確定「場」是正確的名稱?它不應該是「圖像」嗎? – Psi

+0

不,我只是複製它。我確實使用了圖像名稱。ill編輯它 – Jose