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.
- props or event
- component and template
- rendered Vue UI
Bind form fields to state
Core syntax or pattern
<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
- 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.
- 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.
- rendered Vue UI
</template>Apply Forms and v-model in a small example and test normal, edge, and failure cases.
- props or event
- component and template
- 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.
<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>{ 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
<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
Quick quiz 2 / 3
Quick quiz 3 / 3
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