My question, is there a way we could create a one .js file and state all extend(s)
? so we don’t need to extend each time we use the validator.
I just ask because my codes becomes lengthy each time I need a validator. I need to extend it first to be usable.
I’m currently using vue & vuetify with vee-validate. The below codes are working fine reference.
template
JavaScript
x
6
1
<ValidationObserver ref="observer" v-slot="{ validate, reset }">
2
<ValidationProvider v-slot="{ errors }" name="Name" rules="required|max:10">
3
<v-text-field v-model="user.name" :error-messages="errors" label="Name" type="text"></v-text-field>
4
</ValidationProvider>
5
</ValidationObserver>
6
script
JavaScript
1
28
28
1
import { confirmed, required, email, max } from "vee-validate/dist/rules";
2
import { extend, ValidationObserver, ValidationProvider, setInteractionMode } from "vee-validate";
3
4
setInteractionMode("eager");
5
6
extend("required", {
7
required,
8
message: "{_field_} can not be empty",
9
});
10
11
extend("confirmed", {
12
params: ["target"],
13
validate(value, { target }) {
14
return value === target;
15
},
16
message: "{_field_} does not match",
17
});
18
19
extend("max", {
20
max,
21
message: "{_field_} may not be greater than {length} characters",
22
});
23
24
extend("email", {
25
email,
26
message: "Email must be valid",
27
});
28
Advertisement
Answer
Yes, you can declare all your configuration in one file, then import that into your main.js. So, you’d have
JavaScript
1
8
1
// validationConfig.js
2
3
import { confirmed, required, email, max } from "vee-validate/dist/rules";
4
5
extend()
6
extend()
7
//etc
8
In main:
JavaScript
1
4
1
// main.js
2
3
import path/to/validationConfig
4