Skip to content

Commit b8095bf

Browse files
posvayyx990803
authored andcommitted
Warn when using v-model with input[type=file] (vuejs#3791)
1 parent 4675728 commit b8095bf

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/platforms/web/compiler/directives/model.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ function genDefaultModel (
108108
if (isNative && needCompositionGuard) {
109109
code = `if($event.target.composing)return;${code}`
110110
}
111+
// inputs with type="file" are read only and setting the input's
112+
// value will throw an error.
113+
if (process.env.NODE_ENV !== 'production' &&
114+
type === 'file') {
115+
warn(
116+
`<${el.tag} v-model="${value}" type="file">:\n` +
117+
'File inputs are read only. You should use @change instead:\n' +
118+
`<${el.tag} @change="${value} = $event.target.files" type="file">`
119+
)
120+
}
111121
addProp(el, 'value', isNative ? `_s(${value})` : `(${value})`)
112122
addHandler(el, event, code, null, true)
113123
if (needCompositionGuard) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Vue from 'vue'
2+
3+
describe('Directive v-model file', () => {
4+
it('warn to use @change instead', () => {
5+
new Vue({
6+
data: {
7+
file: ''
8+
},
9+
template: '<input v-model="file" type="file">'
10+
}).$mount()
11+
expect('use @change instead').toHaveBeenWarned()
12+
})
13+
})

0 commit comments

Comments
 (0)