Appearance
Vuelidate Plugin
File Information
- Path:
plugins/vuelidate.js - Purpose: Form validation library integration
Overview
The Vuelidate plugin integrates the Vuelidate validation library into the Vue application. Vuelidate provides declarative validation for Vue forms with a simple, reactive API. The plugin makes Vuelidate available globally for all components.
Key Features
Declarative Validation
- Validation rules in component
- Reactive validation state
- Error message access
Built-in Validators
- Required
- Min/Max length
- Numeric
- URL
- And more
Custom Validators
- Custom validation functions
- Async validation
- Conditional validation
Usage Examples
Basic Validation
vue
<template>
<v-form @submit.prevent="handleSubmit">
<v-text-field
v-model="$v.form.email.$model"
:error="$v.form.email.$error"
:error-messages="emailErrors"
/>
<v-btn :disabled="$v.$invalid" @click="handleSubmit">
Submit
</v-btn>
</v-form>
</template>
<script>
import { required, email } from 'vuelidate/lib/validators'
export default {
data() {
return {
form: {
email: ''
}
}
},
validations: {
form: {
email: {
required,
email
}
}
},
computed: {
emailErrors() {
const errors = []
if (!this.$v.form.email.$dirty) return errors
if (!this.$v.form.email.required) errors.push('Email is required')
if (!this.$v.form.email.email) errors.push('Email must be valid')
return errors
}
},
methods: {
handleSubmit() {
this.$v.$touch()
if (!this.$v.$invalid) {
// Submit form
}
}
}
}
</script>Multiple Validators
javascript
validations: {
form: {
password: {
required,
minLength: minLength(6),
maxLength: maxLength(20)
},
confirmPassword: {
required,
sameAs: sameAs('password')
}
}
}Validation Properties
Vuelidate provides reactive properties:
$invalid- Form is invalid$dirty- Field has been touched$error- Field has error$model- Two-way binding value$touch()- Mark field as touched$reset()- Reset validation state
Built-in Validators
Common validators:
required- Field is requiredemail- Valid email formatminLength(n)- Minimum lengthmaxLength(n)- Maximum lengthnumeric- Numeric valueurl- Valid URLsameAs(ref)- Same as another fieldalpha- Alphabetic onlyalphaNum- Alphanumeric
Integration Points
- Form Components: Form validation
- Login Pages: Authentication forms
- Settings Pages: Configuration forms
- Upload Forms: File upload validation
Notes for Development
- Plugin registers Vuelidate globally
- Validation rules in
validationsoption - Use
$vto access validation state - Touch fields to show errors
- Reset validation when needed
Related Documentation
- Vuelidate Documentation - Official Vuelidate docs
- Login Page - Form validation example