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

test($compile): #7048 warn if both v-model and v-bind:value used on same element #7056

Merged
merged 4 commits into from
Nov 16, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/platforms/web/compiler/directives/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default function model (
const modifiers = dir.modifiers
const tag = el.tag
const type = el.attrsMap.type
const attrsMap = el.attrsMap

if (process.env.NODE_ENV !== 'production') {
// inputs with type="file" are read only and setting the input's
Expand All @@ -31,6 +32,20 @@ export default function model (
`File inputs are read only. Use a v-on:change listener instead.`
)
}

// warn if v-bind:value conflicts with v-model
if (
(attrsMap['v-bind:value'] || attrsMap[':value']) &&
type !== 'checkbox' &&
type !== 'radio' &&
tag !== 'select'
) {
const vBindValue = attrsMap['v-bind:value'] ? 'v-bind:value' : ':value'
warn(
`${vBindValue} conflicts with v-model on the same element ` +
'because the latter already expands to a value binding internally'
)
}
}

if (el.component) {
Expand Down
26 changes: 26 additions & 0 deletions test/unit/features/directives/model-text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,32 @@ describe('Directive v-model text', () => {
expect('You are binding v-model directly to a v-for iteration alias').toHaveBeenWarned()
})

it('warn if v-model and v-bind:value conflict', () => {
new Vue({
data: {
test: 'foo'
},
template: '<input type="text" v-model="test" v-bind:value="test"/>'
}).$mount()
expect(
'v-bind:value conflicts with v-model on the same element because the latter already ' +
'expands to a value binding internally'
).toHaveBeenWarned()
})

it('warn if v-model and :value conflict', () => {
new Vue({
data: {
test: 'foo'
},
template: '<input type="text" v-model="test" :value="test"/>'
}).$mount()
expect(
':value conflicts with v-model on the same element because the latter already ' +
'expands to a value binding internally'
).toHaveBeenWarned()
})

if (!isAndroid) {
it('does not trigger extra input events with single compositionend', () => {
const spy = jasmine.createSpy()
Expand Down