Skip to content

UInput: nullable and optional modelModifiers turn any falsy value to null/undefined #5950

Description

@LouisHaftmann

Environment

Operating system macOS 25.2.0
CPU Apple M2 Pro (10 cores)
Node.js version v24.7.0
nuxt/cli version 3.31.2
Package manager pnpm@10.28.1
Nuxt version 4.2.2
Nitro version 2.13.0
Builder vite@7.2.7
Config -
Modules -

Is this bug related to Nuxt or Vue?

Nuxt

Package

v4.x

Version

v4.4.0

Reproduction

https://codesandbox.io/p/devbox/cocky-driscoll-6nnfkz?workspaceId=ws_VuFaVxsxE8szJ3rPbps3cW

Description

The current implementation of model modifiers always sets the output value to null/undefined if the input value is falsy:

function updateInput(value: string | null | undefined) {
  if (props.modelModifiers?.trim) {
    value = value?.trim() ?? null
  }

  if (props.modelModifiers?.number || props.type === 'number') {
    value = looseToNumber(value)
  }

  if (props.modelModifiers?.nullable) {
    value ||= null // 👈
  }

  if (props.modelModifiers?.optional) {
    value ||= undefined // 👈
  }

  modelValue.value = value as T
  emitFormInput()
}

I think these modifiers should only act on empty strings as turning 0 to null/undefined is very unexpected behavior.

I would be happy to implement these changes in a PR.

Additional context

No response

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageAwaiting initial review and prioritization

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions