Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions js/components/dcf-file-size-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export default class DCFFileSizeValidator {

sizeLimit = -1;

validationFailedClass = null;

constructor(fileInput, options) {

if ('errorElementClassList' in options && Array.isArray(options.errorElementClassList)) {
Expand All @@ -25,6 +27,7 @@ export default class DCFFileSizeValidator {

// Get the max size limit
this.sizeLimit = this.#parseSize(this.fileInputElement.dataset.maxSize);
this.validationFailedClass = this.fileInputElement.dataset.validationFailedClass;

this.formattedSizeOutputs.forEach((singleOutputElement) => {
singleOutputElement.innerHTML = this.#formatSize(this.sizeLimit);
Expand All @@ -39,6 +42,9 @@ export default class DCFFileSizeValidator {
// Run logic when the file input's file changes
this.fileInputElement.addEventListener('change', () => {
this.errorElement.classList.add('dcf-d-none!');
if (this.validationFailedClass !== null) {
this.fileInputElement.classList.remove(this.validationFailedClass);
}

// There is no file so we are good
if (this.fileInputElement.files.length === 0) {
Expand All @@ -47,9 +53,16 @@ export default class DCFFileSizeValidator {

// File is too big do not keep it and show error
if (this.fileInputElement.files[0].size > this.sizeLimit) {
this.errorElement.innerText = `The file '${this.fileInputElement.files[0].name}' is too large!`;
this.errorElement.classList.remove('dcf-d-none!');
const filename = this.fileInputElement.files[0].name;

this.fileInputElement.value = '';
if (this.validationFailedClass !== null) {
this.fileInputElement.classList.add(this.validationFailedClass);
}
this.fileInputElement.dispatchEvent(new CustomEvent('change'));

this.errorElement.innerText = `The file '${filename}' is too large!`;
this.errorElement.classList.remove('dcf-d-none!');
return;
}
});
Expand Down