Handling forms and user input is one of the most essential aspects of building interactive applications. Vue.js provides several features that simplify working with forms, including two-way data binding, form input types, validation, and form submission handling.
Vue.js makes form input handling seamless by leveraging its powerful two-way data binding feature. This feature ensures that any changes in the form input are immediately reflected in the data model and vice versa, without requiring manual DOM manipulation.
Vue provides v-model
, a directive that binds input elements like text fields, checkboxes, radios, and more to the Vue instance’s data. Let’s break down how this works in practice.
The simplest form of input handling in Vue is through the v-model
directive. This directive automatically sets up two-way data binding between the input element and a Vue instance’s data property.
Your message: {{ message }}
v-model
directive binds the input field to the message
property in the Vue instance.message
property is automatically updated.{{ message }}
interpolation is updated in real time to reflect the current value.Vue’s v-model
can handle various types of input fields like text, checkboxes, radio buttons, and more. Let’s explore how to work with each of these.
Text input is the most common type of form field, which we’ve already covered in the basic example above.
Checkboxes can be handled easily using v-model
. You can bind a boolean value or an array for multiple checkboxes.
Checked: {{ checked }}
checked
data property is bound to the checkbox. When the checkbox is toggled, checked
updates automatically.For radio buttons, you can bind a single value to the group of radio inputs using v-model
.
Picked: {{ picked }}
v-model
directive binds the picked
data property to the selected radio button.picked
property updates accordingly.Vue.js makes it easy to handle <select>
dropdowns as well. You can bind a v-model
directive to the selected value.
Selected: {{ selected }}
v-model
directive binds the selected
data property to the selected option in the dropdown.If you have a group of checkboxes and want to bind them to an array, Vue makes this easy.
Checked names: {{ checkedNames }}
checkedNames
array is updated whenever a checkbox is checked or unchecked.Vue.js provides a set of modifiers that help you fine-tune how v-model
works. Some commonly used input modifiers include:
.lazy
: Updates the model only after the change
event, not after every input..trim
: Automatically trims the user input (removes whitespace)..number
: Automatically converts the input value to a number.
Message: {{ message }}
Trimmed Text: {{ text }}
Age: {{ age }}
.lazy
: Updates message
only after the input loses focus..trim
: Automatically removes leading and trailing spaces from the text
input..number
: Converts the input into a numeric value for age
.Handling form submissions in Vue is straightforward. You can prevent the default behavior of form submission using Vue’s event modifiers.
Form submitted with name: {{ name }}
@submit.prevent
directive prevents the form’s default submission behavior.submitForm
method is executed when the form is submitted, displaying an alert with the user’s name.Vue.js doesn’t have built-in form validation, but you can easily implement it using custom logic.
checkForm
method checks whether the name
field is empty.In more complex applications, it’s useful to encapsulate form inputs into reusable components. Vue.js allows you to create custom form components that can handle inputs and validations.
Vue.component('input-text', {
props: ['value'],
template: `
`
});
new Vue({
el: '#app',
data: {
userInput: ''
}
});
input-text
component accepts a value
prop and emits an input
event to communicate with the parent.v-model
on custom components.Vue.js provides a comprehensive and efficient way to handle form inputs using v-model. From simple text inputs to complex form validation, Vue.js makes it easy to bind data, handle form submission, and validate input. By leveraging custom components and Vue’s input modifiers, you can build complex and reusable form components with ease. Happy Coding!❤️