Subscribe on changes!

Behavior change v-model on input type number cast value to number by defualt

avatar
Jan 18th 2022

Version

3.2.26

Reproduction link

jsfiddle.net/8vqaotbx/1/

Steps to reproduce

create input element type number the value cast to number type On Vue version 2 it not do it by defualt , it is big chage i have big project that expected the value return is string

What is expected?

Mybe can add to core modifier numeric that not cast the value to number. Or add flag that Vue not do it by defualt

What is actually happening?

v-model cast value of input number to number type


https://github.com/vuejs/core/blob/9c304bfe7942a20264235865b4bb5f6e53fdee0d/packages/runtime-dom/src/directives/vModel.ts#L52

avatar
Jan 19th 2022

This seem to be the a documented feature for vue 3, which is not vue 2 .

image

as a workaround for this use case, you can use .trim modifier to get a string

 <input type="number" v-model.trim="phoneNumber">
avatar
Jan 19th 2022

@lidlanca Thank for the replay and solution it work

But i think that the cast not need to be by default is different behavior

Also the documented in not clear "Vue can automatically convert..." mybe "Vue automatically try to convert..."

avatar
Jan 19th 2022

perhaps vue should not have casted by default, keep this issue open, see what core team has to say.

feel free to suggest improvement to the docs if you find the current phrasing confusing https://github.com/vuejs/docs/issues

also note that the docs will soon be updated to this, which I find even more misleading. image source

avatar
Jan 19th 2022

This was working properly in earlier 3.x versions but then the breaking change was introduced (https://github.com/vuejs/core/issues/3813, https://github.com/vuejs/core/blob/main/CHANGELOG.md#320-beta2-2021-07-19)

avatar
Feb 9th 2023

I was also affected by this change.

Until now, the length of the string was used to detect that the input filed was empty. After the change, if the input field is empty it will return a string type and if it is not empty it will return a number type. In the case of type=number, there was a change to cast to a number as default, but the type is not unified.