Thuta Learning
Vue.js
BasicWeb DevelopmentBeginner

Forms and v-model

Vue.jsLesson 920 min

What you'll walk away with

  • Bind form fields to state
  • Use the trim, number, and lazy modifiers

Explain it simply

Forms and v-model, in plain language

v-model links an input's value to your reactive state. .trim strips whitespace, and .number converts the value to a number. Always validate your form clearly before submitting.

Problem → Why → Solution

The problem — why this concept matters

Assuming a checkbox's value is always a string

A common mistake

Treating client-side validation alone as security validation

Core concept — understand how it works

Use v-model to two-way bind text, checkbox, and select inputs to reactive state.

Mental model
  1. props or event
  2. component and template
  3. rendered Vue UI

Bind form fields to state

Core syntax or pattern

vue
<script setup>
import { reactive } from 'vue'
const form = reactive({ name: '', age: 18, role: 'student', agreed: false })
</script>

<template>
  <input v-model.trim="form.name" placeholder="Name">
  <input v-model.number="form.age" type="number">
  <select v-model="form.role">
    <option value="student">Student</option>
    <option value="teacher">Teacher</option>
  </select>
  <label><input v-model="form.agreed" type="checkbox"> Agree</label>
</template>

Use v-model to two-way bind text, checkbox, and select inputs to reactive state.

Read the code step by step

  1. props or event<script setup>

    v-model links an input's value to your reactive state. .trim strips whitespace, and .number converts the value to a number. Always validate your form clearly before submitting.

  2. component and template<input v-model.number="form.age" type="number">

    Use v-model to two-way bind text, checkbox, and select inputs to reactive state.

  3. rendered Vue UI</template>

    Apply Forms and v-model in a small example and test normal, edge, and failure cases.

Execution flow
  1. props or event
  2. component and template
  3. rendered Vue UI

Success

Bind form fields to state

Failure

Assuming a checkbox's value is always a string

Let's break it down simply

v-model links an input's value to your reactive state. .trim strips whitespace, and .number converts the value to a number. Always validate your form clearly before submitting.

vue
<script setup>
import { reactive } from 'vue'
const form = reactive({ name: '', age: 18, role: 'student', agreed: false })
</script>

<template>
  <input v-model.trim="form.name" placeholder="Name">
  <input v-model.number="form.age" type="number">
  <select v-model="form.role">
    <option value="student">Student</option>
    <option value="teacher">Teacher</option>
  </select>
  <label><input v-model="form.agreed" type="checkbox"> Agree</label>
</template>
You should see
{ name: 'Mya', age: 21, role: 'student', agreed: true }

Try it yourself

Build an enrollment form with name, email, and a course select, and check for empty fields.

Form Input Bindings — Vue.js

Real-world usage

How to apply it in a real project

Apply Forms and v-model in a small example and test normal, edge, and failure cases.

Mini challenge

Build it yourself

Build an enrollment form with name, email, and a course select, and check for empty fields.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
vue
<script setup>
import { reactive } from 'vue'
const form = reactive({ name: '', age: 18, role: 'student', agreed: false })
</script>

<template>
  <input v-model.trim="form.name" placeholder="Name">
  <input v-model.number="form.age" type="number">
  <select v-model="form.role">
    <option value="student">Student</option>
    <option value="teacher">Teacher</option>
  </select>
  <label><input v-model="form.agreed" type="checkbox"> Agree</label>
</template>

Expected: Bind form fields to state

Show hint

Apply Forms and v-model in a small example and test normal, edge, and failure cases.

Quick quiz 1 / 3

What best describes the core idea of Forms and v-model?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • props or event → component and template → rendered Vue UI
  • Use v-model to two-way bind text, checkbox, and select inputs to reactive state.
  • Assuming a checkbox's value is always a string
  • Apply Forms and v-model in a small example and test normal, edge, and failure cases.

Form Input Bindings — Vue.js

Easy traps

  • Assuming a checkbox's value is always a string
  • Treating client-side validation alone as security validation

Exercise

Build an enrollment form with name, email, and a course select, and check for empty fields.

You'll know it worked when: { name: 'Mya', age: 21, role: 'student', agreed: true }

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Forms and v-model | Thuta Learning