Skip to content

Commit

Permalink
Warn when using v-model with input[type=file] (vuejs#3791)
Browse files Browse the repository at this point in the history
  • Loading branch information
posva authored and yyx990803 committed Sep 27, 2016
1 parent 4675728 commit b8095bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/platforms/web/compiler/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ function genDefaultModel (
if (isNative && needCompositionGuard) {
code = `if($event.target.composing)return;${code}`
}
// inputs with type="file" are read only and setting the input's
// value will throw an error.
if (process.env.NODE_ENV !== 'production' &&
type === 'file') {
warn(
`<${el.tag} v-model="${value}" type="file">:\n` +
'File inputs are read only. You should use @change instead:\n' +
`<${el.tag} @change="${value} = $event.target.files" type="file">`
)
}
addProp(el, 'value', isNative ? `_s(${value})` : `(${value})`)
addHandler(el, event, code, null, true)
if (needCompositionGuard) {
Expand Down
13 changes: 13 additions & 0 deletions test/unit/features/directives/model-file.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Vue from 'vue'

describe('Directive v-model file', () => {
it('warn to use @change instead', () => {
new Vue({
data: {
file: ''
},
template: '<input v-model="file" type="file">'
}).$mount()
expect('use @change instead').toHaveBeenWarned()
})
})

0 comments on commit b8095bf

Please sign in to comment.