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.
max
We can use the max
rule to make sure the entered value doesn’t exceed the specified length.
For instance, we can write:
<template>
<Form @submit="onSubmit" v-slot="{ errors }">
<Field name="field" rules="max:10" />
<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 add the max length after the colon.
So if we entered something that’s more than 10 characters long, an error will be displayed.
Also, we can pass an object into the rules
prop to add the same rule:
<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](https://medium.com/r/?url=http%3A%2F%2Ftwitter.com%2Fvee "Twitter profile for @vee")-validate/rules";
Object.keys(rules).forEach((rule) => {
defineRule(rule, rules[rule]);
});
export default {
components: {
Form,
Field,
},
data() {
return {
validations: { max: 10 },
};
},
methods: {
onSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
},
};
</script>
max_value
The max_value
prop lets us make sure that the inputted value doesn’t exceed the given number.
For example, we can use it by writing:
<template>
<Form @submit="onSubmit" v-slot="{ errors }">
<Field name="field" rules="max_value:10" />
<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 add the max number to check for after the colon.
If we enter a number bigger than 10, we’ll see an error displayed.
Also, we can use an object to add the rule:
<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: { max_value: 10 },
};
},
methods: {
onSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
},
};
</script>
mimes
We can use the mimes
rule to check that the selected file has the given MIME type.
For instance, we can write:
<template>
<Form @submit="onSubmit" v-slot="{ errors }">
<Field name="field" type="file" rules="mimes:image/jpeg" />
<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 rule to a file input.
We can also add the rule with an object:
<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: { mimes: ["image/jpeg"] },
};
},
methods: {
onSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
},
};
</script>
We put the MIME type we allow in the mimes
array property.
Conclusion
We can validate MIME types of files, max length of input, and max value of an input within our Vue 3 app with Vee-Validate 4.