Handling Forms and Validation in Vue.js Using VeeValidate | asadmukhtar.info
Step-by-Step Guide to Setting Up Authentication in Laravel 12 with Breeze   |   Manual Authentication in Laravel 12: Step-by-Step Guide   |   How to Build a REST API in Laravel 12 with Sanctum   |   Laravel 12 CRUD Application with Image Upload   |   Laravel 12 Multi-Auth System: Admin & User Login   |   How to Integrate Stripe Payment Gateway in Laravel 12   |   Building a Role-Based Access Control (RBAC) in Laravel 12   |   How to Use Laravel 12 Queues and Jobs for Background Tasks   |   Laravel 12 Livewire CRUD Example with Validation   |   Email Verification and Password Reset in Laravel 12   |   How to Use Laravel 12 API with Vue.js 3   |   Laravel 12 AJAX CRUD with jQuery and Bootstrap   |   Laravel 12 Multi-Language Website Setup   |   React Best Practices for 2025: Performance, SEO, and Scalability   |   How to Build a Full-Stack MERN App: A Step-by-Step Guide   |   React State Management: Redux vs. Context API vs. Recoil   |   Server-Side Rendering (SSR) in React with Next.js for SEO   |   How to Optimize React Apps for Faster Load Times   |   Building a REST API with Node.js and Express for a React App   |   Integrating JWT Authentication in React and Node.js (MERN Stack)   |   Real-time Chat App with React, Node.js, and Socket.io   |   How to Deploy a MERN Stack Application on AWS or Vercel   |   Connecting React Frontend to a Node.js Backend with Axios   |   Laravel Implement Flash Messages Example   |   How to integrate Angular 19 with Node.js and Express for full-stack development   |   Best practices for connecting Angular 19 frontend with Laravel API   |   Step-by-step guide to upgrading an existing project to Angular 19   |   How to implement authentication in Angular 19 using JWT and Firebase   |   Optimizing server-side rendering in Angular 19 with route-level render modes   |   Using Angular 19 signals for state management in large applications   |   How to create standalone components in Angular 19 for modular architecture   |   Building a CRUD application in Angular 19 with MongoDB and Express   |   Implementing lazy loading in Angular 19 to improve performance   |   How to integrate Angular 19 with GraphQL for efficient data fetching   |   Vue 3 Composition API vs Options API: A Comprehensive Comparison   |   Fetching and Displaying Data from APIs in Vue.js with Axios   |   Building a Todo App in Vue.js with Local Storage Integration   |   Handling Forms and Validation in Vue.js Using VeeValidate   |   State Management in Vue.js Applications Using Vuex   |   10 Most Important Tasks Every MERN Stack Developer Should Master   |   How to Build a Full-Stack CRUD App with MERN Stack   |   Best Practices for Authentication & Authorization in MERN Stack   |   1. MEAN Stack vs. MERN Stack: Which One Should You Choose in 2025   |   Top 10 Node.js Best Practices for Scalable and Secure Applications   |   How to Build a REST API with Laravel and Node.js (Step-by-Step Guide)   |   Mastering Angular and Express.js for Full-Stack Web Development   |   Top 10 Daily Tasks Every Frontend Developer Should Practice   |   Essential Backend Development Tasks to Boost Your Coding Skills   |   Real-World Mini Projects for Practicing React.js Daily   |   Laravel Developer Task List: Beginner to Advanced Challenges   |   How to Assign Effective Tasks to Your Intern Developers   |   10 Must-Try Tasks to Master JavaScript Fundamentals   |   Practical CSS Challenges That Improve Your UI Design Skills   |   Top Tasks to Learn API Integration in React and Angular   |   Best Task Ideas for a 30-Day Web Development Challenge   |   Top Git and GitHub Tasks Every Developer Should Know   |   30-Day Task Plan for Web Development Interns   |   Weekly Task Schedule for Junior Developers in a Startup   |   How to Track Progress with Development Tasks for Interns   |   What Tasks Should You Give to Interns in a MERN Stack Project   |   Build These 5 Projects to Master React Routing   |   Task-Based Learning: Become a Full-Stack Developer in 90 Days   |   Daily Coding Tasks That Will Sharpen Your Logical Thinking   |   Top 7 Backend Task Ideas to Practice With Node.js and MongoDB   |  

Handling Forms and Validation in Vue.js Using VeeValidate

Handling forms and validation is a crucial part of web development. Vue.js provides a flexible framework for building dynamic applications, and with the help of VeeValidate, a popular form validation library for Vue, managing form validation becomes much easier. VeeValidate helps you validate user inputs, display error messages, and ensure that data entered into forms meets the required standards before submission.

In this guide, we'll go step by step to implement form handling and validation in a Vue.js application using VeeValidate. By the end, you'll know how to set up form validation, display error messages, and improve user experience in Vue.js apps.

1. Setting Up the Vue Project

If you don't already have a Vue.js project, create one using Vue CLI or Vite. If you're starting from scratch, you can do this by running:

npm install -g @vue/cli
vue create vee-validate-app
cd vee-validate-app
npm run serve

2. Installing VeeValidate

To install VeeValidate in your Vue project, run the following command:

npm install vee-validate

3. Setting Up the Form and Importing VeeValidate

In the component where you want to handle form validation, you’ll need to import VeeValidate and the validation schema. For this example, we’ll create a simple form with a name and email field.

Example:

<template>
  <div>
    <h1>Registration Form</h1>
    <form @submit.prevent="submitForm">
      <div>
        <label for="name">Name:</label>
        <input 
          v-model="name" 
          id="name" 
          name="name" 
          type="text" 
          v-validate="'required|min:3'" 
          :class="{'is-invalid': errors.has('name')}" />
        <span v-if="errors.has('name')" class="error">{{ errors.first('name') }}</span>
      </div>

      <div>
        <label for="email">Email:</label>
        <input 
          v-model="email" 
          id="email" 
          name="email" 
          type="email" 
          v-validate="'required|email'" 
          :class="{'is-invalid': errors.has('email')}" />
        <span v-if="errors.has('email')" class="error">{{ errors.first('email') }}</span>
      </div>

      <button type="submit" :disabled="!isFormValid">Submit</button>
    </form>
  </div>
</template>

<script>
import { required, email, min } from "vee-validate";
import { extend, validate } from "vee-validate";

export default {
  data() {
    return {
      name: "",
      email: ""
    };
  },
  computed: {
    isFormValid() {
      return this.$refs.form && !this.errors.any();
    }
  },
  methods: {
    submitForm() {
      alert("Form Submitted Successfully!");
    }
  },
  mounted() {
    extend("required", required);
    extend("email", email);
    extend("min", min);
  }
};
</script>

<style scoped>
.is-invalid {
  border: 2px solid red;
}

.error {
  color: red;
  font-size: 12px;
}
</style>

4. Understanding the Code

  • v-validate directive: This is the key directive provided by VeeValidate that you can attach to form fields. In this example, we're validating the name and email fields using the required rule and others like min:3 for the name and email for the email field.

    • v-validate="'required|min:3'": Ensures the field is required and that the name field must have a minimum length of 3 characters.
    • v-validate="'required|email'": Ensures the field is required and that the value must be a valid email.
  • errors.has('fieldName'): This checks whether there are validation errors for the specified field. If the field is invalid, we apply the is-invalid class and display the first error message for the user.

  • isFormValid: This computed property checks whether the form is valid by making sure that there are no errors. If there are validation errors, the submit button will be disabled.

5. Displaying Errors

We display errors by checking if a field has errors using errors.has(fieldName). If an error exists, we display it using errors.first(fieldName), which shows the first error message associated with the field.

6. Submitting the Form

When the form is submitted, the submitForm method is triggered. In this example, we simply display an alert to indicate that the form was successfully submitted. In a real-world application, you would typically send the form data to a backend server for processing.

7. Testing the Form

After everything is set up, run the app:

npm run serve

Now, open your browser and navigate to the app. Test the form by:

  • Submitting it with empty fields to see the validation errors.
  • Entering incorrect data (e.g., an invalid email address) to trigger validation.
  • Filling out the form correctly to see it submit successfully.

Conclusion

In this guide, we've covered how to handle forms and form validation in Vue.js using the VeeValidate library. By using VeeValidate, you can easily manage form validation, display error messages, and prevent invalid form submissions. This approach enhances the user experience by providing instant feedback and ensuring data integrity before sending it to the server. VeeValidate is a powerful library that simplifies validation logic, and with Vue.js, it fits perfectly into your form handling workflows.


Related Tutorials

Vue 3 Composition API vs Options API: A Comprehensive Comparison
Fetching and Displaying Data from APIs in Vue.js with Axios
Building a Todo App in Vue.js with Local Storage Integration
Handling Forms and Validation in Vue.js Using VeeValidate
State Management in Vue.js Applications Using Vuex