Form validation is an important part of any app.
In this article, we’ll look at how to use Vee-Validate 4 in our Vue 3 app for form validation.
ext
The ext
validation rule checks that the file selected has one of the extensions listed.
For example, we can write:
<template>
<Form @submit="onSubmit" v-slot="{ errors }">
<Field name="field" type="file" rules="ext:jpg,png" />
<span>{{ errors.field }}</span>
</Form>
</template>
<script>
import { Form, Field, defineRule } from "vee-validate";
import * as rules from "@vee-validate/rules";
Object.keys(rules).forEach((rule) => {
defineRule(rule, rules[rule]);
});
export default {
components: {
Form,
Field,
},
methods: {
onSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
},
};
</script>
Then the selected file must either have the jpg
or png
extensions.
We can add any number of extensions to the list.
Also, we can write:
<template>
<Form @submit="onSubmit" v-slot="{ errors }">
<Field name="field" type="file" :rules="validations" />
<span>{{ errors.field }}</span>
</Form>
</template>
<script>
import { Form, Field, defineRule } from "vee-validate";
import * as rules from "@vee-validate/rules";
Object.keys(rules).forEach((rule) => {
defineRule(rule, rules[rule]);
});
export default {
components: {
Form,
Field,
},
data() {
return {
validations: { ext: ["jpg", "png"] },
};
},
methods: {
onSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
},
};
</script>
We have the extensions in an array.
image
The image
rule lets validate that the selected file has a MIME-type that starts with image/
.
For instance, we can write:
<template>
<Form @submit="onSubmit" v-slot="{ errors }">
<Field name="field" type="file" rules="image" />
<span>{{ errors.field }}</span>
</Form>
</template>
<script>
import { Form, Field, defineRule } from "vee-validate";
import * as rules from "@vee-validate/rules";
Object.keys(rules).forEach((rule) => {
defineRule(rule, rules[rule]);
});
export default {
components: {
Form,
Field,
},
methods: {
onSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
},
};
</script>
We set the rules
prop to 'image'
to restrict the selected file to be an image.
Also, we can write:
<template>
<Form @submit="onSubmit" v-slot="{ errors }">
<Field name="field" type="file" :rules="validations" />
<span>{{ errors.field }}</span>
</Form>
</template>
<script>
import { Form, Field, defineRule } from "vee-validate";
import * as rules from "@vee-validate/rules";
Object.keys(rules).forEach((rule) => {
defineRule(rule, rules[rule]);
});
export default {
components: {
Form,
Field,
},
data() {
return {
validations: { image: true },
};
},
methods: {
onSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
},
};
</script>
to do the same thing.
integer
The integer
rule lets us validate that the inputted value must be an integer.
For instance, we can write:
<template>
<Form @submit="onSubmit" v-slot="{ errors }">
<Field name="field" rules="integer" />
<span>{{ errors.field }}</span>
</Form>
</template>
<script>
import { Form, Field, defineRule } from "vee-validate";
import * as rules from "@vee-validate/rules";
Object.keys(rules).forEach((rule) => {
defineRule(rule, rules[rule]);
});
export default {
components: {
Form,
Field,
},
methods: {
onSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
},
};
</script>
to add the integer
rule to the rules
prop.
Also, we can write:
<template>
<Form @submit="onSubmit" v-slot="{ errors }">
<Field name="field" :rules="validations" />
<span>{{ errors.field }}</span>
</Form>
</template>
<script>
import { Form, Field, defineRule } from "vee-validate";
import * as rules from "@vee-validate/rules";
Object.keys(rules).forEach((rule) => {
defineRule(rule, rules[rule]);
});
export default {
components: {
Form,
Field,
},
data() {
return {
validations: { integer: true },
};
},
methods: {
onSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
},
};
</script>
to do the same thing.
Conclusion
We can validate that the selected file has the given extension or that it’s an image with Vee-Validate 4 in our Vue 3 app.
Also, we can validate that what the user entered is an integer.