Skip to content

Commit fefbfd0

Browse files
committed
Fix FileType validation issues
1 parent be6c611 commit fefbfd0

File tree

1 file changed

+56
-18
lines changed

1 file changed

+56
-18
lines changed

src/components/QuestionTypes/FileType.vue

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,10 @@
2828
extends: TextType,
2929
3030
name: QuestionType.File,
31-
32-
mounted() {
33-
if (this.question.accept) {
34-
this.mimeTypeRegex = new RegExp(this.question.accept.replace('*', '[^\\/,]+'))
35-
}
36-
},
3731
3832
methods: {
3933
setAnswer(answer) {
40-
this.question.setAnswer(this.files)
34+
this.question.setAnswer(this.$refs.input.files)
4135
4236
this.answer = answer
4337
this.question.answered = this.isValid()
@@ -49,15 +43,31 @@
4943
return this.errorMessage !== null
5044
},
5145
52-
validate() {
53-
this.errorMessage = null
46+
checkFileAccept(file) {
47+
const extension = '.' + file.name.split('.').pop()
48+
49+
if (this.acceptedFileExtensionsRegex && this.acceptedFileExtensionsRegex.test(extension)) {
50+
return true
51+
}
52+
53+
if (this.acceptedFileMimesRegex && this.acceptedFileMimesRegex.test(file.type)) {
54+
return true
55+
}
5456
57+
return false
58+
},
59+
60+
validate() {
5561
if (this.question.required && !this.hasValue) {
5662
return false
5763
}
5864
65+
const
66+
files = this.$refs.input.files,
67+
numFiles = files.length
68+
5969
if (this.question.accept) {
60-
if (!Array.from(this.files).every(file => this.mimeTypeRegex.test(file.type))) {
70+
if (!Array.from(files).every(file => this.checkFileAccept(file))) {
6171
this.errorMessage = this.language.formatString(this.language.errorAllowedFileTypes, {
6272
fileTypes: this.question.accept
6373
})
@@ -67,17 +77,15 @@
6777
}
6878
6979
if (this.question.multiple) {
70-
const fileCount = this.files.length
71-
72-
if (this.question.min !== null && fileCount < +this.question.min) {
80+
if (this.question.min !== null && numFiles < +this.question.min) {
7381
this.errorMessage = this.language.formatString(this.language.errorMinFiles, {
7482
min: this.question.min
7583
})
7684
7785
return false
7886
}
7987
80-
if (this.question.max !== null && fileCount > +this.question.max) {
88+
if (this.question.max !== null && numFiles > +this.question.max) {
8189
this.errorMessage = this.language.formatString(this.language.errorMaxFiles, {
8290
max: this.question.max
8391
})
@@ -88,7 +96,7 @@
8896
8997
if (this.question.maxSize !== null) {
9098
const fileSize =
91-
Array.from(this.files).reduce((current, file) => current + file.size, 0)
99+
Array.from(files).reduce((current, file) => current + file.size, 0)
92100
93101
if (fileSize > +this.question.maxSize) {
94102
this.errorMessage = this.language.formatString(this.language.errorMaxFileSize, {
@@ -99,14 +107,44 @@
99107
}
100108
}
101109
110+
this.errorMessage = null
111+
102112
return this.$refs.input.checkValidity()
103113
}
104114
},
105115
106116
computed: {
107-
files() {
108-
return this.$refs.input.files
109-
}
117+
acceptArray() {
118+
if (this.question.accept) {
119+
return this.question.accept.split(',')
120+
}
121+
122+
return []
123+
},
124+
125+
acceptedFileMimes() {
126+
return this.acceptArray.filter(accept => accept[0] !== '.')
127+
},
128+
129+
acceptedFileMimesRegex() {
130+
if (this.acceptedFileMimes.length) {
131+
return new RegExp(this.acceptedFileMimes.join('|').replace(/\*/g, '.\*'))
132+
}
133+
134+
return null
135+
},
136+
137+
acceptedFileExtensions() {
138+
return this.acceptArray.filter(accept => accept[0] === '.')
139+
},
140+
141+
acceptedFileExtensionsRegex() {
142+
if (this.acceptedFileExtensions.length) {
143+
return new RegExp(this.acceptedFileExtensions.join('|'))
144+
}
145+
146+
return null
147+
}
110148
}
111149
}
112150
</script>

0 commit comments

Comments
 (0)