Skip to content

Add .nullify modifier to vue-model #9693

@serge1peshcoff

Description

@serge1peshcoff

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: #3666 (comment)
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" />

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions