Vue Academy · Lesson

VeeValidate for Schema-Based Validation

useForm(), useField(), yup schemas, error messages, handling async validation.

Lesson 4 of 413 steps

VeeValidate for Schema-Based Validation is a free Vue Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, and your progress syncs across the web and the CoddyKit app. The Vue Academy course includes 4 lessons in total.

Scaling Up with VeeValidate

As forms grow, hand-written validation becomes tedious. VeeValidate is a popular Vue library that manages form state, runs validation against a schema, and exposes errors — all reactively.

Schema-Based Validation

Instead of writing checks by hand, you describe rules declaratively in a schema. Pairing VeeValidate with yup lets you build that schema fluently.

<script setup>
import * as yup from 'yup'
const schema = yup.object({
  name: yup.string().required(),
  email: yup.string().email().required()
})
</script>

Setting Up the Form with useForm

useForm initializes form-level state and validation. Pass your schema as validationSchema and it wires everything together.

<script setup>
import { useForm } from 'vee-validate'
const { handleSubmit, errors } = useForm({
  validationSchema: schema
})
</script>

Connecting a Field with useField

useField registers one input by name. It returns the field value, its errorMessage, and a handleBlur handler.

<script setup>
import { useField } from 'vee-validate'
const { value: name, errorMessage, handleBlur } = useField('name')
</script>

Binding the Field in the Template

Bind the returned value with v-model and wire @blur to handleBlur for on-blur validation.

<template>
  <input v-model="name" @blur="handleBlur" />
  <span>{{ errorMessage }}</span>
</template>

Displaying the Error Message

Each field exposes its own errorMessage, which is a reactive string. It is empty when valid and holds the schema message when invalid.

<template>
  <input v-model="name" @blur="handleBlur" />
  <span v-if="errorMessage" class="error">{{ errorMessage }}</span>
</template>

Handling Submission

handleSubmit wraps your submit function. It runs validation first and only calls your callback with the values if the whole form is valid.

<script setup>
const onSubmit = handleSubmit((values) => {
  console.log('valid form', values)
})
</script>

<template>
  <form @submit="onSubmit">...</form>
</template>

Form-Level Errors

The errors object from useForm maps field names to their current messages, handy for a summary or conditional UI.

<script setup>
const { errors } = useForm({ validationSchema: schema })
// errors.name, errors.email
</script>

Why Use a Library

  • Declarative schemas replace repetitive manual checks.
  • Built-in blur, change, and submit validation timing.
  • Per-field errorMessage and form-level errors.
  • Easy async and cross-field rules through yup.

Components vs Composition API

VeeValidate also offers <Form> and <Field> components for a template-first style. The composition functions useForm and useField fit better with <script setup> and give more control.

Manual vs Schema-Based

Manual validation teaches the mechanics and suits tiny forms. Schema-based validation with VeeValidate and yup scales to large, complex forms with far less code and fewer bugs.

Quick Check

Test your VeeValidate knowledge.

Recap

Schema-based validation with VeeValidate:

  • Describe rules in a yup schema passed as validationSchema.
  • useForm manages form state, errors, and handleSubmit.
  • useField returns value, errorMessage, and handleBlur per input.
  • It scales far better than manual validation for large forms.
Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
42
Lessons
156

Frequently Asked Questions

Is the “VeeValidate for Schema-Based Validation” lesson free?

Yes — any 3 lessons of the Vue Academy learning path, including “VeeValidate for Schema-Based Validation”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Vue Academy course includes 4 lessons in total.

What will I learn in “VeeValidate for Schema-Based Validation”?

useForm(), useField(), yup schemas, error messages, handling async validation. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “VeeValidate for Schema-Based Validation” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. v-model on All Form Elements
  2. Form Submission and Reset
  3. Manual Validation Patterns
  4. VeeValidate for Schema-Based Validation
← Back to Vue Academy