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

input type="number" should not need an extra attribute “number” #1713

Closed
sdebacker opened this issue Nov 4, 2015 · 13 comments
Closed

input type="number" should not need an extra attribute “number” #1713

sdebacker opened this issue Nov 4, 2015 · 13 comments

Comments

@sdebacker
Copy link

It should be great if we could write

<input type="number">

in place of

<input type="number" number>

What do you think?

@yyx990803 yyx990803 changed the title [Feature] input type="number" should not need an extra attribute “number” input type="number" should not need an extra attribute “number” Nov 5, 2015
@yyx990803 yyx990803 modified the milestone: 1.1.0 Nov 6, 2015
@Morgul
Copy link

Morgul commented Nov 8, 2015

👍 This bit me the other day; took me 45 minutes to figure out what was going on.

@azamat-sharapov
Copy link

Sounds like "magic", maybe just warning would be helpful then?

@Morgul
Copy link

Morgul commented Nov 8, 2015

@azamat-sharapov I'm not entirely sure what about it seems "magical". I expect v-model not to mess with data types, keeping it whatever type I set. Instead, when used on an input with type="number", the input is coerced to string; not something I would expect it to do.

Thinking about it another way, I have already said, "This is a number" by declaring type="number". Why isn't that sufficient? There's no magic here; Vue is just paying attention to information the user's already given about their intentions.

@yyx990803
Copy link
Member

@Morgul turns out <input type="number">'s raw value is... still a string. So yeah, Vue is not messing with the data type and preserving the browser's default behavior. It only casts the type when you ask for it with the number param attribute.

@Morgul
Copy link

Morgul commented Nov 8, 2015

@yyx990803 Ah, I wasn't aware of that; haven't actually worked with raw input fields in a very long time. Still, it feels like this is an obvious feature for v-model.

@azamat-sharapov
Copy link

I was calling auto-casting as "magical", because as Evan also said - it's all string. It's HTML to blame. So, if you want enhancement to standard HTML, just add Vue's number param or cast it by yourself. Just my opinion.

@yyx990803 yyx990803 removed this from the 1.1.0 milestone Dec 8, 2015
@yyx990803
Copy link
Member

I think v-model should try to stay close to native behavior, and only do value conversions when the user explicitly wants it. So we'll keep the current behavior.

@Morgul
Copy link

Morgul commented Dec 9, 2015

@yyx990803 Would you be open to an alternative syntax?

<!-- Cast to number -->
<input type="number" v-model.number="foo">

<!-- Cast to boolean -->
<input type="text" v-model.boolean="bar">

This feels more in keeping with v1.0's syntax.

@yyx990803
Copy link
Member

@Morgul That sounds like a good idea! You should open a separate issue.

@kgrosvenor
Copy link

@yyx990803 Evan please see #5731 regarding a simular issue/workaround which does not involve autocasting, but instead splicing the attribute when its a number and empty....

@zero298
Copy link

zero298 commented Apr 6, 2018

Just in case someone else stumbles across this, this is now addressed per @Morgul suggestion and documented here: Form Input Bindings/Modifiers/.number. Just hoping to save the next person the step of having to go find it in the docs themselves.

@erik127
Copy link

erik127 commented May 16, 2018

Thanks for the update @zero298 , I just stumbled across this. And thanks everyone for the good work

@danielgindi
Copy link

Browser's behavior is that a numeric input has an additional property valueAsNumber which should be used instead of value. And is numeric by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants