如何在上传图片按钮上进行必填项

时间:2017-05-04 06:20:25

标签: javascript jquery laravel

我想要图像上传按钮 这是我视图中的代码

模态

<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title" id="myModalLabel">Unggah Gambar</h4>
  </div>

  <form id="upload-image" enctype="multipart/form-data">
    <div class="modal-body">
      <input id="input-dim-1" name="inputdim1[]" type="file" multiple class="file-loading" accept="image/*">
    </div>
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">Keluar</button>
    </div>
  </form>
</div>

$("#input-dim-1").fileinput({
      uploadExtraData: {_token:"{{csrf_token()}}", key : key},
      uploadUrl: "{!! route('admin.product.getupload') !!}",
      allowedFileExtensions: ["jpg", "png", "jpeg"],
      minImageWidth: 50,
      minImageHeight: 50,
      maxFileCount: 5
    });

的Ajax

$('#form-product').submit(function(event) {
     if ($("#input-dim-1").val() == '') {
       event.preventDefault();
       alert("Image Is Required");
     } else {
       form.submit();
       }
       return false;
     });

现在当用户没有上传图片时会出现所需内容,但是当用户上传图片时也会出现这种情况:( 有人能帮助我吗?

感谢

1 个答案:

答案 0 :(得分:0)

你有:

<input id="input-dim-1" name="inputdim1[]" type="file" multiple class="file-loading" accept="image/*">

使用laravel时的后端,你可以像这样验证数组输入:

'inputdim1.*' => 'required'

Html方面你可以添加必需的属性:

<input id="input-dim-1" name="inputdim1[]" type="file" multiple class="file-loading" accept="image/*" required>