question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add .nullify modifier to vue-model

See original GitHub issue

What 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:closed
  • Created 5 years ago
  • Reactions:19
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

13reactions
serge1peshcoffcommented, Mar 13, 2019

I totally support the idea of custom modifiers, but the built-in .nullify modifier can be quite useful to others, what do you think?

12reactions
posvacommented, Mar 13, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditionally add .trim modifier in Vue.js Input field
Making an input for this (isTrim ) in the component. <input v-model.trim="msg">. Can we add conditionally above trim with the component input?
Read more >
What you should know about Vue v-model - LearnVue
The .lazy modifier changes our v-model so it only syncs after change events. The change event is triggered when a change is commited....
Read more >
Vue JS 3 Tutorial - 21 - Modifiers - YouTube
Courses - https://learn.codevolution.dev/ Support UPI - https://support.codevolution.dev/ Support PayPal ...
Read more >
v-model breaking - Vue 3 Migration Guide
BREAKING: v-bind 's .sync modifier and component model option are removed and replaced with ... NEW: Added the ability to create custom v-model...
Read more >
Vue Directive & Event Modifiers - Coding Explained -
Because of our modifier, event propagation is stopped from here, and thus the click event on the div element is ignored. I will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found