Chapter 38: Vue Form Inputs
Form Inputs — one of the most practical, most frequently written, and most misunderstood parts of building real Vue applications.
Form inputs are where users actually interact with your app — typing names, selecting options, checking boxes, uploading files, choosing dates… Vue makes this feel natural and powerful thanks to v-model (two-way binding), but there are many small but important details that separate beginner code from clean, maintainable, production-ready forms.
We’ll cover everything step by step — from basic text input to complex custom components — using modern Vue 3 + <script setup> + TypeScript style (what you should use in 2026).
1. The Heart of Vue Forms: v-model (Two-Way Binding)
v-model is syntactic sugar that automatically handles:
- :value=”variable” (push data down to input)
- @input=”variable = $event.target.value” (pull changes up)
Vue chooses the correct prop and event based on the element type.
| Input Type | v-model uses… | Default event | Modifiers you often use | Notes / Gotchas |
|---|---|---|---|---|
| <input type=”text”> | value prop | input | .trim, .lazy, .number | Most common |
| <textarea> | value | input | .trim, .lazy | Same as text |
| <input type=”checkbox”> | checked | change | — | Boolean or array |
| <input type=”radio”> | checked | change | — | String/number |
| <select> | value | change | — | String or array (multiple) |
| <input type=”number”> | valueAsNumber | input | .number (recommended) | Avoid without modifier |
| Custom component | modelValue (default) | update:modelValue | .lazy, .number, .trim | v-model support |
2. Basic Form – Login Example (Text + Password + Checkbox)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
<!-- src/views/LoginView.vue --> <template> <form @submit.prevent="handleLogin" class="login-form"> <div class="field"> <label for="email">Email</label> <input id="email" type="email" v-model.trim="form.email" placeholder="you@company.com" required autocomplete="email" /> <small v-if="form.email && !isValidEmail" class="error"> Please enter a valid email </small> </div> <div class="field"> <label for="password">Password</label> <div class="password-wrapper"> <input id="password" :type="showPassword ? 'text' : 'password'" v-model.trim="form.password" placeholder="••••••••" required minlength="8" /> <button type="button" class="toggle-password" @click="showPassword = !showPassword" tabindex="-1" > {{ showPassword ? 'Hide' : 'Show' }} </button> </div> <small v-if="form.password.length > 0 && form.password.length < 8" class="error"> Minimum 8 characters </small> </div> <div class="field checkbox"> <input id="remember" type="checkbox" v-model="form.rememberMe" /> <label for="remember">Remember me</label> </div> <button type="submit" :disabled="loading || !formIsValid"> {{ loading ? 'Logging in…' : 'Login' }} </button> <p v-if="serverError" class="server-error">{{ serverError }}</p> </form> </template> <script setup lang="ts"> import { reactive, ref, computed } from 'vue' const form = reactive({ email: '', password: '', rememberMe: true }) const showPassword = ref(false) const loading = ref(false) const serverError = ref<string | null>(null) const isValidEmail = computed(() => { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(form.email) }) const formIsValid = computed(() => { return ( form.email && isValidEmail.value && form.password.length >= 8 ) }) async function handleLogin() { if (!formIsValid.value) return loading.value = true serverError.value = null try { // Fake API call await new Promise(r => setTimeout(r, 1200)) console.log('Login success:', form) alert('Welcome back!') // router.push('/dashboard') } catch (err: any) { serverError.value = err.message || 'Login failed. Please try again.' } finally { loading.value = false } } </script> <style scoped> .login-form { max-width: 420px; margin: 3rem auto; padding: 2rem; background: white; border-radius: 12px; box-shadow: 0 4px 20px rgba(0,0,0,0.1); } .field { margin-bottom: 1.5rem; } label { display: block; margin-bottom: 0.5rem; font-weight: 500; } input:not([type="checkbox"]) { width: 100%; padding: 0.8rem; border: 1px solid #d1d5db; border-radius: 6px; font-size: 1rem; } input:focus { outline: none; border-color: #3b82f6; box-shadow: 0 0 0 3px rgba(59,130,246,0.1); } .password-wrapper { position: relative; } .toggle-password { position: absolute; right: 0.8rem; top: 50%; transform: translateY(-50%); background: none; border: none; color: #3b82f6; cursor: pointer; font-size: 0.95rem; } .checkbox { display: flex; align-items: center; gap: 0.5rem; } button[type="submit"] { width: 100%; padding: 0.9rem; background: #3b82f6; color: white; border: none; border-radius: 6px; font-weight: 600; cursor: pointer; margin-top: 1rem; } button:disabled { opacity: 0.6; cursor: not-allowed; } .error, .server-error { color: #dc2626; font-size: 0.9rem; margin-top: 0.4rem; } </style> |
3. Other Input Types – Quick Reference
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<!-- Checkbox (single → boolean) --> <input type="checkbox" v-model="form.newsletter" /> <!-- Multiple checkboxes (array) --> <div v-for="interest in interests" :key="interest"> <label> <input type="checkbox" :value="interest" v-model="form.interests" /> {{ interest }} </label> </div> <!-- Radio buttons --> <div> <label><input type="radio" v-model="form.gender" value="male" /> Male</label> <label><input type="radio" v-model="form.gender" value="female" /> Female</label> </div> <!-- Select single --> <select v-model="form.country"> <option value="">Select country</option> <option value="IN">India</option> <option value="US">USA</option> </select> <!-- Select multiple --> <select multiple v-model="form.languages"> <option>English</option> <option>Hindi</option> <option>Telugu</option> </select> <!-- Number input (always use .number modifier) --> <input type="number" v-model.number="form.age" min="18" max="120" /> <!-- File input (special – no v-model) --> <input type="file" @change="handleFileUpload" multiple /> |
4. Custom Form Inputs (v-model on Components)
Make your own reusable inputs with full v-model support.
CustomInput.vue
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<template> <div class="custom-field"> <label :for="id">{{ label }}</label> <input :id="id" :type="type" :value="modelValue" @input="$emit('update:modelValue', $event.target.value)" :placeholder="placeholder" :class="{ 'error-border': error }" /> <small v-if="error" class="error-text">{{ error }}</small> </div> </template> <script setup lang="ts"> defineProps<{ modelValue: string label: string type?: string placeholder?: string error?: string }>() defineEmits<{ (e: 'update:modelValue', value: string): void }>() const id = `field-${Math.random().toString(36).slice(2)}` </script> <style scoped> .error-border { border-color: #ef4444 !important; } .error-text { color: #ef4444; font-size: 0.85rem; margin-top: 0.3rem; } </style> |
Usage in parent form
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
<CustomInput v-model="form.email" label="Email Address" type="email" placeholder="you@company.com" :error="form.email && !isValidEmail ? 'Invalid email' : ''" /> |
Quick Summary Table – Vue Form Inputs 2026 Cheat Sheet
| Input Type | v-model Behavior | Common Modifiers / Props | Validation Tip |
|---|---|---|---|
| text / email / tel | string | .trim, .lazy | regex + blur |
| password | string | .trim + show/hide toggle | minlength |
| checkbox (single) | boolean | — | required |
| checkbox (multiple) | array | :value | min/max length |
| radio | string / number | :value | required |
| select (single) | string / number | — | required |
| select (multiple) | array | multiple | min/max length |
| number | number (with .number) | .number mandatory | min/max/step |
| file | no v-model – use @change | multiple, accept | FileReader API |
| date / datetime-local | string (YYYY-MM-DD) | — | HTML5 validation |
Pro Tips from Real Projects (Hyderabad 2026)
- Use reactive({}) for form objects — easier than 10 separate refs
- Always add autocomplete attributes (accessibility + password managers)
- Use .trim + .lazy on text inputs → cleaner data
- For complex forms → consider VeeValidate 4, FormKit, or Vorms (Composition API friendly)
- File uploads → use FormData + fetch/axios — show progress bar
- Debounce heavy inputs (search) → use watch + setTimeout
Your mini practice task:
- Build a signup form with email, password, confirm password, country select, interests checkboxes
- Add real-time validation (email regex, password match, required fields)
- Disable submit until valid
- On submit → console.log form data
Any part confusing? Want full examples for:
- File upload with preview + progress?
- Custom date picker with v-model?
- VeeValidate 4 + Yup schema example?
- Multi-step wizard form?
Just tell me — we’ll build the next production-ready form together 🚀
Happy form-building from Hyderabad! 💙
