28
28
extends: TextType,
29
29
30
30
name: QuestionType .File ,
31
-
32
- mounted () {
33
- if (this .question .accept ) {
34
- this .mimeTypeRegex = new RegExp (this .question .accept .replace (' *' , ' [^\\ /,]+' ))
35
- }
36
- },
37
31
38
32
methods: {
39
33
setAnswer (answer ) {
40
- this .question .setAnswer (this .files )
34
+ this .question .setAnswer (this .$refs . input . files )
41
35
42
36
this .answer = answer
43
37
this .question .answered = this .isValid ()
49
43
return this .errorMessage !== null
50
44
},
51
45
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
+ }
54
56
57
+ return false
58
+ },
59
+
60
+ validate () {
55
61
if (this .question .required && ! this .hasValue ) {
56
62
return false
57
63
}
58
64
65
+ const
66
+ files = this .$refs .input .files ,
67
+ numFiles = files .length
68
+
59
69
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))) {
61
71
this .errorMessage = this .language .formatString (this .language .errorAllowedFileTypes , {
62
72
fileTypes: this .question .accept
63
73
})
67
77
}
68
78
69
79
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 ) {
73
81
this .errorMessage = this .language .formatString (this .language .errorMinFiles , {
74
82
min: this .question .min
75
83
})
76
84
77
85
return false
78
86
}
79
87
80
- if (this .question .max !== null && fileCount > + this .question .max ) {
88
+ if (this .question .max !== null && numFiles > + this .question .max ) {
81
89
this .errorMessage = this .language .formatString (this .language .errorMaxFiles , {
82
90
max: this .question .max
83
91
})
88
96
89
97
if (this .question .maxSize !== null ) {
90
98
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 )
92
100
93
101
if (fileSize > + this .question .maxSize ) {
94
102
this .errorMessage = this .language .formatString (this .language .errorMaxFileSize , {
99
107
}
100
108
}
101
109
110
+ this .errorMessage = null
111
+
102
112
return this .$refs .input .checkValidity ()
103
113
}
104
114
},
105
115
106
116
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
+ }
110
148
}
111
149
}
112
150
</script >
0 commit comments