Для интеграции VeeValidate с Vuetify необходимо установить библиотеку и ее зависимости:
npm install vee-validate @vee-validate/rules @vee-validate/i18n
После установки подключение происходит в основном файле приложения
(main.js или main.ts):
import { createApp } from 'vue';
import App from './App.vue';
import { Field, Form, ErrorMessage, defineRule, configure } from 'vee-validate';
import * as rules from '@vee-validate/rules';
import { localize } from '@vee-validate/i18n';
import vuetify from './plugins/vuetify';
Object.keys(rules).forEach(rule => {
defineRule(rule, rules[rule]);
});
configure({
generateMessage: localize('ru'),
validateOnInput: true,
});
const app = createApp(App);
app.component('VForm', Form);
app.component('VField', Field);
app.component('ErrorMessage', ErrorMessage);
app.use(vuetify).mount('#app');
Form, Field,
ErrorMessage регистрируются для использования в шаблонах
Vue.Для корректной работы с компонентами Vuetify
(v-text-field, v-select,
v-checkbox) необходимо привязать валидацию через
v-model и события input. Основной шаблон
выглядит так:
<VForm @submit="handleSubmit">
<VField name="email" rules="required|email" v-slot="{ field, errors }">
<v-text-field
v-bind="field"
label="Email"
:error="errors.length > 0"
:error-messages="errors"
/>
</VField>
<VField name="password" rules="required|min:8" v-slot="{ field, errors }">
<v-text-field
v-bind="field"
type="password"
label="Пароль"
:error="errors.length > 0"
:error-messages="errors"
/>
</VField>
<v-btn type="submit" color="primary">Войти</v-btn>
</VForm>
field, который содержит value и события
onInput, и массив errors.Если используется кастомный компонент, необходимо передавать
modelValue и событие update:modelValue для
корректной синхронизации с VeeValidate:
<VField name="username" rules="required|min:3" v-slot="{ field, errors }">
<custom-input
v-model="field.value"
:error="errors.length > 0"
:error-messages="errors"
/>
</VField>
Динамическая установка правил валидации:
<VField
name="phone"
:rules="phoneRequired ? 'required|numeric' : 'numeric'"
v-slot="{ field, errors }"
>
<v-text-field
v-bind="field"
label="Телефон"
:error="errors.length > 0"
:error-messages="errors"
/>
</VField>
rules может изменяться реактивно, что
позволяет включать или отключать обязательность полей.Для проверки всей формы используется метод validate() из
VeeValidate:
import { useForm } from 'vee-validate';
setup() {
const { handleSubmit, validate } = useForm();
const onSub mit = handleSubmit(values => {
console.log('Данные формы:', values);
});
const checkForm = async () => {
const result = await validate();
if (!result.valid) {
console.log('Ошибки формы:', result.errors);
}
};
return { onSubmit, checkForm };
}
valid
и errors, что позволяет программно управлять логикой
формы.VeeValidate поддерживает локализацию через
@vee-validate/i18n. Для русского языка:
import { localize } from '@vee-validate/i18n';
import ru from '@vee-validate/i18n/dist/locale/ru.json';
configure({
generateMessage: localize({ ru }),
validateOnInput: true,
});
configure({
generateMessage: localize({
ru: {
messages: {
required: 'Поле {field} обязательно для заполнения',
},
},
}),
});
Для форм, где количество полей динамическое (например, массив
контактов), VeeValidate позволяет использовать
FieldArray:
<template>
<VForm @submit="onSubmit">
<div v-for="(contact, index) in contacts" :key="index">
<VField :name="`contacts[${index}].email`" rules="required|email" v-slot="{ field, errors }">
<v-text-field
v-bind="field"
label="Email контакта"
:error="errors.length > 0"
:error-messages="errors"
/>
</VField>
</div>
<v-btn type="submit">Отправить</v-btn>
</VForm>
</template>
<script>
import { reactive } from 'vue';
export default {
setup() {
const contacts = reactive([{ email: '' }, { email: '' }]);
const onSub mit = values => {
console.log(values);
};
return { contacts, onSubmit };
},
};
</script>
name позволяет
привязать правила к массиву полей.Для единообразного отображения ошибок в компонентах Vuetify рекомендуется использовать:
.v-input .v-messages__message {
color: red;
font-size: 0.875rem;
}
v-messages).append или prepend-inner.Для блокировки кнопки при наличии ошибок:
<v-btn
type="submit"
color="primary"
:disabled="!formIsValid"
>
Отправить
</v-btn>
import { useForm } from 'vee-validate';
setup() {
const { meta } = useForm();
const formIsValid = computed(() => meta.value.valid);
return { formIsValid };
}
meta.valid обновляется автоматически при изменении
полей.