π Vue Form Input Binding β A Complete Guide (2025)
π§² Introduction β What Is Form Input Binding in Vue?
Form input binding in Vue.js is all about keeping the input values in sync with your componentβs data. Vue provides powerful and intuitive tools like v-model
, v-bind
, and event handlers to seamlessly connect forms with logic.
π― In this guide, youβll learn:
- How to bind form inputs using
v-model
andv-bind
- Handle text, checkboxes, radios, selects, and custom inputs
- Use modifiers for advanced binding control
- Best practices for reactive forms
π What Is Input Binding in Vue?
Input binding refers to synchronizing user input with your Vue component’s data
. This ensures that any changes in the form fields are automatically reflected in your component and vice versa.
π Two-Way Binding with v-model
The most common way to bind form inputs is with v-model
. This enables two-way data binding, meaning:
- Updating the input updates the data.
- Updating the data updates the input.
π§ͺ Example β Text Input:
<input v-model="name" placeholder="Enter your name" />
<p>Hello, {{ name }}</p>
data() {
return { name: '' };
}
β Binding Checkbox Inputs
π Single Checkbox β Boolean Value:
<input type="checkbox" v-model="accepted" />
<label>I accept the terms</label>
data() {
return { accepted: false };
}
π§ Result: accepted
becomes true
or false
.
π§ͺ Multiple Checkboxes β Array Binding:
<label><input type="checkbox" v-model="skills" value="HTML"> HTML</label>
<label><input type="checkbox" v-model="skills" value="CSS"> CSS</label>
data() {
return { skills: [] };
}
π§ Result: skills
is an array like ['HTML', 'CSS']
.
π Radio Button Input Binding
<input type="radio" v-model="gender" value="Male"> Male
<input type="radio" v-model="gender" value="Female"> Female
data() {
return { gender: '' };
}
π§ Result: gender
holds the selected radio value.
π½ Select Dropdown Input Binding
<select v-model="framework">
<option disabled value="">Select one</option>
<option>Vue</option>
<option>React</option>
<option>Angular</option>
</select>
```js
data() {
return { framework: '' };
}
π§ Works for both single and multiple select options.
π Using v-bind
on Form Attributes
If you need to dynamically bind placeholder
, disabled
, or readonly
:
<input :placeholder="namePlaceholder" :disabled="isDisabled" />
data() {
return {
namePlaceholder: 'Enter your name',
isDisabled: false
};
}
ποΈ Modifiers for Form Inputs
Vue provides useful modifiers for form input control:
Modifier | Description |
---|---|
.lazy | Updates on change instead of input |
.trim | Trims whitespace |
.number | Casts input to number |
Example:
<input v-model.lazy.trim="username" />
<input v-model.number="age" />
π§ Custom Components with v-model
To make custom inputs compatible with v-model
, use modelValue
and emit update:modelValue
:
Parent:
<CustomInput v-model="email" />
CustomInput.vue:
<template>
<input :value="modelValue" @input="$emit('update:modelValue', $event.target.value)" />
</template>
<script>
export default {
props: ['modelValue']
}
</script>
π§ Enables two-way binding for component-based form controls.
π§° Real-World Example β Registration Form
<template>
<form @submit.prevent="register">
<input v-model="name" placeholder="Name" />
<input v-model="email" type="email" placeholder="Email" />
<select v-model="role">
<option>Admin</option>
<option>User</option>
</select>
<button>Register</button>
</form>
</template>
<script>
export default {
data() {
return {
name: '',
email: '',
role: 'User'
};
},
methods: {
register() {
alert(`Registered ${this.name} as ${this.role}`);
}
}
}
</script>
π Summary β Recap & Next Steps
Vue form input binding is efficient and elegant thanks to v-model
, v-bind
, and built-in modifiers. With minimal code, you can maintain dynamic form UIs that sync perfectly with your application data.
π Key Takeaways:
- Use
v-model
for two-way input binding - Use
v-bind
for binding attributes likeplaceholder
,disabled
- Support for input, checkbox, radio, select, and custom components
- Modifiers offer granular control (
.lazy
,.number
,.trim
)
βοΈ Real-World Relevance:
From login pages to dynamic surveys and complex dashboardsβVueβs form input bindings are at the core of modern Vue app interactivity.
β FAQ Section
β What is v-model
used for?
β It creates a two-way binding between form inputs and Vue data, updating both instantly.
β Can I bind multiple checkboxes to an array?
β
Yes. Just use the same v-model
and set different value
attributes:
<input type="checkbox" v-model="choices" value="A">
β How do I bind dynamic attributes like disabled
or placeholder
?
β
Use v-bind
or its shorthand:
<input :disabled="isDisabled" :placeholder="hint">
β What’s the difference between v-bind
and v-model
?
β
v-bind
binds one-way to attributes; v-model
binds two-way between input and data.
β Can v-model
be used with custom Vue components?
β
Yes, using modelValue
and emitting update:modelValue
.
Share Now :