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

View File

@ -116,7 +116,7 @@ function addForm(){
</div>
<div class="col-md-6 mb-2">
<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>
${klasifikasiDok.map(kla => `
<option value="${kla?.master_klasifikasi_directory_id}/${kla?.nama_klasifikasi_directory}">${kla?.nama_klasifikasi_directory}</option>

View File

@ -14,7 +14,7 @@
<!-- Body -->
<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">
<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>
</div>
<div id="file-preview"