Mastering Vue 3: A Comprehensive Guide to Building Modern Web Applications <Part 14 />
Hany Taha
Posted on March 12, 2024
Content:
1. Mastering Vue 3 - Part 1 [Introduction and key features]
2. Mastering Vue 3 - Part 2 [Benefits of using Vue 3 for web development]
3. Mastering Vue 3 - Part 3 [Template syntax in Vue 3]
4. Mastering Vue 3 - Part 4 [Reactivity Fundamentals]
5. Mastering Vue 3 - Part 5 [Class and Style Bindings]
6. Mastering Vue 3 - Part 6 [Lifecycle Hooks]
7. Mastering Vue 3 - Part 7 [Understanding components]
8. Mastering Vue 3 - Part 8 [Installing Vue project and file structure]
9. Mastering Vue 3 - Part 9 [Vue Router in Vue 3]
10. Mastering Vue 3 - Part 10 [Animation in Vue 3]
11. Mastering Vue 3 - Part 11 [State Management with Pinia]
12. Mastering Vue 3 - Part 12 [Teleport in Vue 3]
13. Mastering Vue 3 - Part 13 [Working with API Calls ]
14. Mastering Vue 3 - Part 14 [Forms and Form Validation ]
Forms and Form Validation
Handling Forms and Form Submissions in Vue 3:
In Vue 3, handling forms and form submissions involves using the v-model directive to bind form inputs to data properties in your component. You can use the v-model
directive to create two-way data binding, allowing you to easily get and set the values of form inputs. Here's an example:
<template>
<form @submit="submitForm">
<input type="text" v-model="name" />
<input type="email" v-model="email" />
<button type="submit">Submit</button>
</form>
</template>
<script>
export default {
data() {
return {
name: '',
email: ''
};
},
methods: {
submitForm() {
// Handle form submission logic here
console.log('Form submitted!', this.name, this.email);
}
}
};
</script>
In this example, we bind the name
and email
input fields to the component's data using v-model
. When the form is submitted, the submitForm
method is called, and the input values can be accessed using this.name
and this.email
.
Implementing Form Validation and Error Handling:
To implement form validation and error handling, you can use various techniques such as computed properties, watchers, or form validation libraries. Here's an example using computed properties to validate the form fields:
<template>
<form @submit="submitForm">
<input type="text" v-model="name" :class="{ 'is-invalid': !isNameValid }" />
<input type="email" v-model="email" :class="{ 'is-invalid': !isEmailValid }" />
<button type="submit">Submit</button>
</form>
</template>
<script>
export default {
data() {
return {
name: '',
email: ''
};
},
computed: {
isNameValid() {
return this.name.length > 0;
},
isEmailValid() {
// Add your email validation logic here
// For example, you can use a regular expression
const emailRegex = /^\S+@\S+\.\S+$/;
return emailRegex.test(this.email);
}
},
methods: {
submitForm() {
if (this.isNameValid && this.isEmailValid) {
// Handle form submission logic here
console.log('Form submitted!', this.name, this.email);
} else {
// Handle form validation errors
console.log('Form validation error!');
}
}
}
};
</script>
In this example, we use computed properties isNameValid
and isEmailValid
to validate the name
and email
fields, respectively. We apply the 'is-invalid'
class to the input fields if the validation fails.
Working with Form Libraries and Custom Form Components:
Vue 3 has several form libraries available that provide advanced form handling and validation features. Some popular options include VeeValidate, Vuelidate, and Vuetify. These libraries can simplify form validation, error handling, and provide additional UI components for building forms.
Alternatively, you can create your own custom form components using Vue's composition API. By encapsulating form logic within reusable components, you can create a more modular and maintainable form solution. For example, you can create a custom TextInput
component that handles form validation internally:
<template>
<div>
<input :type="type" :value="value" @input="updateValue" :class="{ 'is-invalid': !isValid }" />
<div v-if="!isValid" class="error-message">{{ errorMessage }}</div>
</div>
</template>
<script>
export default {
props: {
type: { type: String, default: 'text' },
value: { type: String, default: '' },
validation: { type: Function, default: () => true },
errorMessage: { type: String, default: 'Invalid value' }
},
computed: {
isValid() {
return this.validation(this.value);
}
},
methods: {
updateValue(event) {
this.$emit('input', event.target.value);
}
}
};
</script>
In this example, we create a TextInput
component that accepts props for type
, value
, validation
, and errorMessage
. The component validates the input value based on the provided validation function, and displays an error message if the validation fails.
By utilizing form libraries or creating custom form components, you can enhance the functionality and reusability of your forms in Vue 3. Choose the approach that best suits your project's requirements and complexity.
Posted on March 12, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
March 12, 2024