Skip to content

Commit f35f7e3

Browse files
committed
add v-model dynamic type warning
1 parent addb461 commit f35f7e3

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

Diff for: src/platforms/web/compiler/directives/model.js

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ export default function model (
1515
const modifiers = dir.modifiers
1616
const tag = el.tag
1717
const type = el.attrsMap.type
18+
if (process.env.NODE_ENV !== 'production') {
19+
const dynamicType = el.attrsMap['v-bind:type'] || el.attrsMap[':type']
20+
if (tag === 'input' && dynamicType) {
21+
warn(
22+
`<input :type="${dynamicType}" v-model="${value}">:\n` +
23+
`v-model does not support dynamic input types. Use v-if branches instead.`
24+
)
25+
}
26+
}
1827
if (tag === 'select') {
1928
return genSelect(el, value)
2029
} else if (tag === 'input' && type === 'checkbox') {

Diff for: test/unit/features/directives/model-dynamic.spec.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Vue from 'vue'
2+
3+
describe('Directive v-model dynamic input type', () => {
4+
it('should warn', function () {
5+
new Vue({
6+
data: {
7+
type: 'text',
8+
text: 'hi'
9+
},
10+
template: `<input :type="type" v-model="text">`
11+
}).$mount()
12+
expect(`v-model does not support dynamic input types`).toHaveBeenWarned()
13+
})
14+
})

0 commit comments

Comments
 (0)