Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Forms Handling  ›  Lesson

v-model Modifiers

Modifiers 11 minThe lazy, number and trim modifiers change when v-model updates and how it cleans the text before storing it.

v-model Modifiers

Shelf's form adds a book with 320 pages, and the "pages left" total becomes '32010'. Someone else saves a title as " Dune " with stray spaces, and the search cannot find it. Both bugs come from raw input text. Three small modifiers on v-model fix them at the source.

The rule

.number converts, .trim cleans, .lazy waits.

<input v-model.trim="title" placeholder="Title" />
<input v-model.number="pages" placeholder="Pages" />
<textarea v-model.lazy="notes"></textarea>
  • .number passes the typed text through parseFloat, so '320' becomes the number 320. If the text cannot be parsed, such as 'abc', the original string is kept, so still validate. An input with type="number" gets this conversion automatically.
  • .trim removes whitespace from both ends: " Dune " is stored as "Dune".
  • .lazy changes when the ref updates. Normally v-model syncs on every input event, every keystroke. With .lazy it syncs on the change event, which for a text box fires when the user leaves the field (or presses Enter in a single-line input). Good for fields that trigger expensive work, like validation that calls a server.

Modifiers combine: v-model.lazy.trim="title" waits until the user leaves, then stores the trimmed text.

Why the '32010' bug happened: in JavaScript, + with a string joins text. '320' + 10 is '32010', while 320 + 10 is 330. Converting at the input means every later calculation gets a real number.

The shortcut

For each field, ask three quick questions: "Is it a number? Could spaces sneak in? Must it update every keystroke?" Yes to the first → .number. Yes to the second → .trim. No to the third → .lazy.

Tuning the Add Book formStep 1 of 4
  1. Title

    Not a number, spaces can sneak in, the live preview needs every keystroke. v-model.trim.

A trap with .number: it does not guarantee a number. A user who types letters leaves a string in the ref. Check with typeof or Number.isFinite before trusting it.

Drills

Answer fast — trust the rule.

quick drill

With v-model.number="pages", the user types 320. What is stored?

quick drill

With v-model.number="pages", the user types abc. What is stored?

quick drill

What does .lazy change?

quick drill

The user types " Circe " into <input v-model.lazy.trim="title"> and tabs away. What is title?

Remember

  • .number parses to a number, .trim removes outer spaces, .lazy updates on change instead of input.
  • Shortcut: ask "number? spaces? every keystroke?" for each field.
  • Trap: .number keeps unparseable text as a string, so still validate.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPages that will not add up

The form stores page counts with <input v-model="pages" /> and later computes pages + 10, getting '32010'. Explain the bug and the fix, and when you would add .trim or .lazy to the title field.