javascript - Vue.js prop validation for props - Stack Overflow

I have a child ponent with the following props declaration:props: {count: Number,maxNum: Number}This is

I have a child ponent with the following props declaration:

   props: {
        count: Number,
        maxNum: Number
   }

This is normally fine, but I also have maxNum mapped to an input field with:

<input type="text" v-model="maxNum" value="maxNum">

So even if the user puts "4" in the input, Vue.js thinks that it's a string, when in reality it's a valid number if parsed right.

I tried doing this, but it didn't fail properly on the input "apple":

   props: {
        maxNum: {
            validator: function (v) {
                parseInt(v);
                return true;
            }
        }
   }

What's the prescribed way of validating props when a v-model is involved?

I have a child ponent with the following props declaration:

   props: {
        count: Number,
        maxNum: Number
   }

This is normally fine, but I also have maxNum mapped to an input field with:

<input type="text" v-model="maxNum" value="maxNum">

So even if the user puts "4" in the input, Vue.js thinks that it's a string, when in reality it's a valid number if parsed right.

I tried doing this, but it didn't fail properly on the input "apple":

   props: {
        maxNum: {
            validator: function (v) {
                parseInt(v);
                return true;
            }
        }
   }

What's the prescribed way of validating props when a v-model is involved?

Share Improve this question asked Apr 23, 2017 at 4:49 lollercoasterlollercoaster 16.5k35 gold badges122 silver badges181 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Ah, so Vue.JS actually provides a nice way to do this apparently.

<input type="text" v-model.number="maxNum" value="maxNum">

The .number modifier lets the v-bind:value part of the v-model equation to treat the input value as a number.

Reference on v-model.number in Vue.JS guide is here.

I see you figured out how to solve your issue, but I'll still try to explain what went wrong here.

Issue 1

I'm guessing you expected parseInt to break on inputs without a number in them, but that's not the case. parseInt(null), parseInt('foo') and parseInt([{ bar: 'Baz' }]) will all work fine, they'll just return NaN. Consequently, your validator will always proceed to the second line and return true, thus treating any input as valid. To fix it, you would have to run parseInt on your input and check if it returns NaN, so your validator would like like this:

validator: function (v) {
    return !isNan(parseInt(v));
}

Issue 2

You're not really looking for a validator. A validator just checks input values and decides whether they are valid or not, it doesn't change them in any way. In other words, the corrected validator above wouldn't cast '13' to 13, it would just prevent input like 'apple' or undefined. '13' would get through unchanged and you would have to manually cast it later on.

The feature you are looking for was provided as an optional coerce function on each ponent prop in Vue 1, but it was removed for version 2. If you ever need to do it in a slightly more plicated manner that isn't covered by the solution you linked to, the officially remended way is to simply use a puted value.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744293947a4567169.html

相关推荐

  • javascript - Vue.js prop validation for props - Stack Overflow

    I have a child ponent with the following props declaration:props: {count: Number,maxNum: Number}This is

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信