Vue.js course ยท Module 5: Events & Forms
Forms - v-model for Mission Data
In this lesson4
The crew registration panel has a name field and a paragraph that repeats the name. For this to work, two connections are needed: the variable has to reach the field, and whatever the operator types has to get back into the variable. Writing that by hand for each of ten fields means twenty bindings and twenty chances for a mistake. Vue closes both directions in a single directive: v-model, a two-way data binding between a form field and a variable.
What v-model does under the hood
v-model is a shorthand. On a text field it expands into two things you already know: the :value binding, which writes the variable into the field, and the @input listener, which after every change reads $event.target.value and saves the result into the variable.
<template>
<input :value="crewName" @input="crewName = $event.target.value" />
<input v-model="crewName" />
</template>
<script setup>
import { ref } from 'vue'
const crewName = ref('')
</script>Both fields work identically and stay in sync with each other: type in one, and the text appears in the other, because both read and write the same ref. crewName remains an ordinary ref, so v-model needs no special state. Remember one rule: v-model ignores the value, checked and selected attributes written in the HTML. The source of truth is always the variable, which is why you set the initial value in JavaScript.
v-model Basics
The same directive works on every form field, but under the hood it picks the property and the event that fit the field type. Text fields and <textarea> use value and the input event, checkboxes and radio buttons use checked and change, and <select> uses value and change. Let us walk through the mission form in stages, starting with text.
<template>
<div class="mission-form" style="background: #0a0e27; color: #00ff88;">
<!-- Text input -->
<input v-model="crewName" placeholder="Crew name" style="background: #1a1e37; color: #00ff88;" />
<p>Crew: {{ crewName }}</p>
<!-- Textarea -->
<textarea v-model="missionNotes" placeholder="Mission notes" style="background: #1a1e37; color: #00ff88;"></textarea>
<p>Notes: {{ missionNotes }}</p>The field and the text area behave the same way: the paragraph below them changes with every letter. Do not put {{ missionNotes }} between the <textarea> tags, because interpolation does not work in that spot. Always attach v-model to a text area. Now the checkboxes.
<!-- Checkbox - boolean -->
<label style="color: #00ff88;">
<input type="checkbox" v-model="isAutomated" />
Automatic mode
</label>
<p>Automatic: {{ isAutomated }}</p>
<!-- Checkbox - array -->
<label style="color: #00ff88;">
<input type="checkbox" v-model="activeSystems" value="oxygen" />
Oxygen generator
</label>
<label style="color: #00ff88;">
<input type="checkbox" v-model="activeSystems" value="water" />
Water recycler
</label>
<p>Active systems: {{ activeSystems }}</p>A single checkbox bound to isAutomated produces a boolean value: true when checked and false when unchecked. When several checkboxes share one array, each has its own value attribute, and activeSystems collects the values of the checked boxes, in the order they were checked. Time for the radio buttons and the drop-down list.
<!-- Radio -->
<label style="color: #00ff88;">
<input type="radio" v-model="powerMode" value="eco" />
Eco
</label>
<label style="color: #00ff88;">
<input type="radio" v-model="powerMode" value="performance" />
Performance
</label>
<p>Power mode: {{ powerMode }}</p>
<!-- Select -->
<select v-model="selectedModule" style="background: #1a1e37; color: #00ff88;">
<option disabled value="">Select module</option>
<option value="alpha">Oxygen Alpha</option>
<option value="beta">Plasma Beta</option>
<option value="gamma">Water Gamma</option>
</select>
<p>Module: {{ selectedModule }}</p>
</div>
</template>The radio group writes the value of the selected button into powerMode, so the variable always holds a single value. <select> does the same with the chosen option. The first option with disabled and an empty value is the recommended pattern: when the variable starts as an empty string, the list shows a prompt to choose, and on iOS it does not block choosing the first real option. The script is all that is left.
<script setup>
import { ref } from 'vue'
const crewName = ref('')
const missionNotes = ref('')
const isAutomated = ref(false)
const activeSystems = ref([])
const powerMode = ref('')
const selectedModule = ref('')
</script>Every field gets a starting value that matches its type: an empty string for the text fields, the radio group and the list, false for the single checkbox and an empty array for the checkbox group. A type mistake, such as ref('') for a checkbox group, is the most common cause of strange form behavior.
Select options from an array
The catalog of station modules keeps growing, so options are not typed by hand. You generate them with the v-for directive from an array of objects, and you provide the option value through the :value binding. As with any v-for list, every option also gets a unique :key.
<template>
<select v-model="selectedModuleId">
<option disabled value="">Select a module</option>
<option v-for="mod in modules" :key="mod.id" :value="mod.id">
{{ mod.name }}
</option>
</select>
<p>Selected id: {{ selectedModuleId }}</p>
</template>
<script setup>
import { ref } from 'vue'
const modules = ref([
{ id: 1, name: 'Oxygen Alpha' },
{ id: 2, name: 'Plasma Beta' }
])
const selectedModuleId = ref('')
</script>After you pick Plasma Beta, the paragraph shows 2. Pay attention to the type: thanks to :value, the variable receives the number 2, exactly the one from the array, while a plain value="2" attribute would always produce text. The :value binding accepts any value, even a whole object.
From data to submission
Every form is built in the same order: first the state in ref or reactive, then binding the fields with v-model, next @submit.prevent on the form, and finally validation before sending. You already know the first three steps, and we will add validation in the lesson on advanced forms. I recommend giving variables names that say what they store rather than what kind of field they belong to: activeSystems survives replacing the checkboxes with a multi-select list, checkboxValues does not. In the next lesson you will tune the signal from the fields with the .lazy, .number and .trim modifiers.
Remember: v-model is a two-way communication channel - the field on the panel and the variable in the mission computer always show the same value, no matter which side changed it.
Code for this lesson: App.vue
<script setup>
import { ref, computed } from 'vue'
const astronautName = ref('')
const missionCode = ref('')
const missionDescription = ref('')
const statusReport = ref('')
const characterCount = computed(() => missionDescription.value.length)
const wordCount = computed(() => {
return missionDescription.value.trim().split(/\s+/).filter(word => word.length > 0).length
})
const isFormValid = computed(() => {
return astronautName.value.length >= 3 &&
missionCode.value.length >= 4 &&
missionDescription.value.length >= 10
})
</script>
<template>
<div class="data-transmission">
<div class="transmission-header">
<h2>Data Transmission System</h2>
<p class="subtitle">v-model - Two-way Data Binding</p>
</div>
<div class="form-container">
<div class="form-group">
<label for="astronaut">Astronaut Name:</label>
<input
id="astronaut"
v-model="astronautName"
type="text"
placeholder="Enter name..."
class="data-input"
/>
<div class="input-feedback">
Entered: <strong>{{ astronautName || '(empty)' }}</strong>
</div>
</div>
<div class="form-group">
<label for="mission-code">Mission Code:</label>
<input
id="mission-code"
v-model="missionCode"
type="text"
placeholder="e.g. MARS-2087-A1"
class="data-input"
/>
<div class="input-feedback">
Code: <strong>{{ missionCode || '(empty)' }}</strong>
</div>
</div>
<div class="form-group">
<label for="description">Mission Description:</label>
<textarea
id="description"
v-model="missionDescription"
placeholder="Describe mission details..."
class="data-textarea"
rows="5"
></textarea>
<div class="textarea-stats">
<span>Characters: {{ characterCount }}</span>
<span>Words: {{ wordCount }}</span>
</div>
</div>
<div class="form-group">
<label for="status">Status Report (optional):</label>
<textarea
id="status"
v-model="statusReport"
placeholder="Additional information..."
class="data-textarea"
rows="3"
></textarea>
</div>
<div class="validation-status" :class="{ valid: isFormValid }">
<span v-if="isFormValid">Form ready for transmission</span>
<span v-else>Fill in all required fields</span>
</div>
</div>
<div class="data-preview">
<h3>Transmitted Data Preview</h3>
<div class="preview-box">
<pre>{{ {
astronaut: astronautName || null,
missionCode: missionCode || null,
description: missionDescription || null,
status: statusReport || null,
valid: isFormValid
} }}</pre>
</div>
</div>
<div class="vmodel-info">
<h3>How v-model works:</h3>
<div class="info-content">
<p><code>v-model</code> is shorthand for:</p>
<code class="code-example">:value="variable" @input="variable = $event.target.value"</code>
<p>Automatically synchronizes data between input and variable!</p>
</div>
</div>
</div>
</template>
<style scoped>
.data-transmission {
background: linear-gradient(135deg, #0a0e27 0%, #1a1e3f 100%);
color: #00ff88;
padding: 30px;
border-radius: 15px;
border: 2px solid #00b4d8;
font-family: 'Courier New', monospace;
max-width: 900px;
margin: 0 auto;
}
.transmission-header {
text-align: center;
margin-bottom: 30px;
padding-bottom: 20px;
border-bottom: 2px solid #00b4d8;
}
.transmission-header h2 {
color: #00ff88;
margin: 0 0 10px 0;
font-size: 28px;
text-shadow: 0 0 10px #00ff88;
}
.subtitle {
color: #00b4d8;
margin: 0;
font-size: 14px;
}
.form-container {
margin-bottom: 30px;
}
.form-group {
margin-bottom: 25px;
}
.form-group label {
display: block;
color: #00b4d8;
margin-bottom: 10px;
font-size: 14px;
font-weight: bold;
}
.data-input,
.data-textarea {
width: 100%;
background: rgba(0, 180, 216, 0.1);
border: 2px solid #00b4d8;
border-radius: 8px;
padding: 12px 15px;
color: #00ff88;
font-size: 16px;
font-family: 'Courier New', monospace;
box-sizing: border-box;
transition: all 0.3s ease;
}
.data-input:focus,
.data-textarea:focus {
outline: none;
border-color: #00ff88;
box-shadow: 0 0 15px rgba(0, 255, 136, 0.3);
background: rgba(0, 180, 216, 0.15);
}
.data-textarea {
resize: vertical;
}
.input-feedback {
margin-top: 8px;
color: #00b4d8;
font-size: 12px;
padding: 8px 12px;
background: rgba(0, 180, 216, 0.05);
border-radius: 4px;
}
.input-feedback strong {
color: #00ff88;
}
.textarea-stats {
margin-top: 8px;
display: flex;
gap: 20px;
color: #00b4d8;
font-size: 12px;
}
.validation-status {
padding: 15px;
background: rgba(255, 107, 53, 0.1);
border: 2px solid #ff6b35;
border-radius: 8px;
text-align: center;
font-weight: bold;
color: #ff6b35;
transition: all 0.3s ease;
}
.validation-status.valid {
background: rgba(0, 255, 136, 0.1);
border-color: #00ff88;
color: #00ff88;
}
.data-preview {
margin-bottom: 30px;
}
.data-preview h3 {
color: #00ff88;
margin-bottom: 15px;
}
.preview-box {
background: rgba(0, 0, 0, 0.4);
border: 1px solid #00b4d8;
border-radius: 8px;
padding: 20px;
overflow-x: auto;
}
.preview-box pre {
margin: 0;
color: #00ff88;
font-size: 14px;
line-height: 1.6;
}
.vmodel-info {
background: rgba(0, 255, 136, 0.05);
padding: 20px;
border-radius: 8px;
border: 1px solid #00ff88;
}
.vmodel-info h3 {
color: #00ff88;
margin-top: 0;
}
.info-content {
color: #00b4d8;
}
.info-content p {
margin: 10px 0;
}
.info-content code {
background: rgba(0, 255, 136, 0.2);
padding: 2px 6px;
border-radius: 4px;
color: #00ff88;
font-size: 13px;
}
.code-example {
display: block;
background: rgba(0, 180, 216, 0.2);
padding: 10px 15px;
border-radius: 6px;
margin: 10px 0;
font-size: 12px;
}
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is v-model in Vue.js?
2. v-model is a shorthand for which combination?
These are 2 of 7 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Use v-model with a text input and display the entered value in a paragraph.
- Horizontal ordering
Arrange the correct syntax for an input with v-model:
- Click in order
Arrange the syntax for a checkbox with v-model:
- Code editor
Use checkbox (array) to select equipment and radio to choose a mission type.
- Vertical ordering
Arrange the steps of creating a reactive Vue form from data to validation:
- Horizontal ordering
Arrange the correct syntax for a select option with v-for:
- Code editor
Build a select with v-for and :value for a list of objects.
Tasks after this lesson
- QuestionWhat is v-model
- Questionv-model as a shorthand
- Coding taskPractice: v-model with text input
- Put in orderv-model syntax
- Questionv-model with checkbox
- Click in orderBuilding a checkbox with v-model
- Questionv-model with multiple checkboxes
- Coding taskPractice: Checkboxes and radio
- Put in orderSteps to create a form with v-model
- Questionv-model with radio button
- QuestionData type from checkbox v-model
- QuestionSelect with dynamic options
- Put in orderSelect option syntax with v-for
- Coding taskPractice: v-model with select