Add .nullify modifier to vue-model
See original GitHub issueWhat problem does this feature solve?
I’m using Vue.js in most of my projects as a frontend framework. And in a lot of cases I’ve faced a pattern like this: the string should be either null or not empty. So for now I need to write a lot of workarounds about it, which result in a lot of copy-paste code in all of my components, since custom modifiers are not implemented. So my proposal is: add the .nullify modifier to v-model, that would set the value to null if it’s an empty string.
I think that can benefit not only me, but also other users, as I’ve seen a lot of upvotes here, where the user suggested it: https://github.com/vuejs/vue/issues/3666#issuecomment-289095184 I can and will make a PR on that if most of the people would think it’s a good idea.
What does the proposed API look like?
<input type="text" v-model.nullify="value" />
Can also work with other modifiers, like this:
<input type="text" v-model.trim.nullify="value" />
That’d give us null if the value
is either empty or contains only tabs and spaces.
Also that can work with numbers (I’ve also faced a few cases where the value should be either a number or null):
<input type="text" v-model.number.nullify="value" />
Issue Analytics
- State:
- Created 5 years ago
- Reactions:19
- Comments:5 (2 by maintainers)
Top GitHub Comments
I totally support the idea of custom modifiers, but the built-in .nullify modifier can be quite useful to others, what do you think?
that would make an argument to add any built-in modifier to v-model Use a computed property for the moment to coerce a text input value to null