fixing form file after submit, sebelumnya ketika di submit saat file nya tidak diinput tidak tervalidasi, sekarang tervalidasi

This commit is contained in:
JokoPrasetio 2025-09-16 15:27:24 +07:00
parent 14216d5784
commit 7aa749216c
3 changed files with 19 additions and 16 deletions

View File

@ -48,23 +48,21 @@
renderFileList(inputId, container); renderFileList(inputId, container);
} }
formCreate.on('submit', function(e){ formCreate.off('submit').on('submit', function(e){
e.preventDefault(); e.preventDefault();
const submitBtn = $(this).find('button[type="submit"]');
let hasFile = false; submitBtn.prop('disabled', true).text('menyimpan...')
for (const inputId in allFiles) { let hasFile = Object.keys(allFiles).every(id => {
if(allFiles[inputId] && allFiles[inputId].length > 0){ console.log('fil ' , allFiles[id],' length', allFiles[id].length > 0);
hasFile = true; return Array.isArray(allFiles[id]) && allFiles[id].length > 0;
break; });
}
}
if(!hasFile){ if(!hasFile){
Swal.fire({ Swal.fire({
icon: 'warning', icon: 'warning',
title: 'Perhatian', title: 'Perhatian',
text: 'Silahkan upload minimal 1 file sebelum submit' text: 'Silahkan upload minimal 1 file sebelum submit'
}); });
submitBtn.prop('disabled', false).text('Simpan')
return; return;
} }
const formData = new FormData(this); const formData = new FormData(this);
@ -93,24 +91,28 @@
color: "#fff", color: "#fff",
} }
}).showToast(); }).showToast();
$("#col_add_file").html(''); allFiles = [];
colCount = 1; // reset counter formCreate.find('input[type="file"].file-input').each(function () {
formCreate.find('input[type="file"]').each(function () {
const newInput = $(this).clone(); // clone dengan attribute multiple const newInput = $(this).clone(); // clone dengan attribute multiple
$(this).replaceWith(newInput); // ganti input lama dengan baru $(this).replaceWith(newInput); // ganti input lama dengan baru
bindFileUpload(newInput[0]) bindFileUpload(newInput[0])
}); // console.log(newInput);
});
colCount = 1;
$("#col_add_file").html('')
formCreate.find('select').val(null).trigger('change'); formCreate.find('select').val(null).trigger('change');
document.querySelectorAll(".file-name").forEach(el => { document.querySelectorAll(".file-name").forEach(el => {
el.classList.add("d-none"); el.classList.add("d-none");
el.innerHTML = ""; el.innerHTML = "";
}); });
if($("#klasifikasi_dok").val().length === 0 || $("#kategori_dok").val().length === 0 ){ if($("#klasifikasi_dok").val().length === 0 || $("#kategori_dok").val().length === 0 ){
index() index()
}else{ }else{
searchData() searchData()
} }
submitBtn.prop('disabled', false).text('Simpan')
modalCreate.removeEventListener('hidden.bs.modal', handler); modalCreate.removeEventListener('hidden.bs.modal', handler);
}; };
modalCreate.addEventListener('hidden.bs.modal', handler); modalCreate.addEventListener('hidden.bs.modal', handler);
@ -126,6 +128,7 @@
title: 'Gagal', title: 'Gagal',
text: err.message text: err.message
}); });
submitBtn.prop('disabled', false).text('Simpan...')
} }
}); });
}); });

View File

@ -116,7 +116,7 @@ function addForm(){
</div> </div>
<div class="col-md-6 mb-2"> <div class="col-md-6 mb-2">
<label>Klasifikasi Dokumen</label> <label>Klasifikasi Dokumen</label>
<select class="form-select" name="data[${colCount}][klasifikasi]"> <select class="form-select" name="data[${colCount}][klasifikasi]" required>
<option value="" disable >Select Choose</option> <option value="" disable >Select Choose</option>
${klasifikasiDok.map(kla => ` ${klasifikasiDok.map(kla => `
<option value="${kla?.master_klasifikasi_directory_id}/${kla?.nama_klasifikasi_directory}">${kla?.nama_klasifikasi_directory}</option> <option value="${kla?.master_klasifikasi_directory_id}/${kla?.nama_klasifikasi_directory}">${kla?.nama_klasifikasi_directory}</option>

View File

@ -14,7 +14,7 @@
<!-- Body --> <!-- Body -->
<div class="modal-body p-2" style="min-height:250px; max-height:70vh; overflow:auto;"> <div class="modal-body p-2" style="min-height:250px; max-height:70vh; overflow:auto;">
<div class="d-flex justify-content-end align-items-center sticky-top bg-white z-10"> <div class="d-flex justify-content-end align-items-center sticky-top bg-white z-10">
<button type="button" class="btn btn-sm btn-outline-danger mb-2 ms-2" id="delete-file">🗑 Hapus</button> <button type="button" class="btn btn-sm btn-outline-danger mb-2 me-2" id="delete-file">🗑 Hapus</button>
<button type="button" class="btn btn-sm btn-outline-primary mb-2" id="download-file"> Download</button> <button type="button" class="btn btn-sm btn-outline-primary mb-2" id="download-file"> Download</button>
</div> </div>
<div id="file-preview" <div id="file-preview"