Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn when using v-model with input[type=file] #3791

Merged
merged 1 commit into from
Sep 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()
})
})