Forms
WARNING
- It is no longer necessary as in the previous
@we/nuxt
version to import theformValidator
mixin, it is integrated within the package.
Click one of the following buttons to see how it is used.
Simple Example
vue
<template>
<v-form ref="form">
<we-select
resource="provinces"
v-model="model.province_id"
:error-messages="serverErrors.province_id"
:rules="rules.province_id"
:label="$t('test.form.fields.province_id.label')"
/>
<v-text-field
v-model="model.string"
:error-messages="serverErrors.string"
:rules="rules.string"
:label="$t('test.form.fields.string.label')"
/>
<v-btn @click="save"> Save </v-btn>
</v-form>
</template>
<script>
const defaultValues = {
province_id: null,
number: 0,
}
export default {
mixins: [formValidator],
props: {
value: {
type: Object,
required: false,
default: undefined
},
},
data: () => ({
model: null,
}),
computed: {
rules () {
return {
province_id: [this.validatorRequired],
number: [],
}
}
},
watch: {
value: {
immediate: true,
handler (val) {
this.resetValidation()
this.model = mergeDefaults(val, defaultValues)
}
}
},
methods: {
resetValidation () {
this.clearServerErrors()
this.$refs.form?.resetValidation()
},
reset () {
this.model = mergeDefaults({}, defaultValues)
this.resetValidation()
},
async save () {
if (!this.$refs.form.validate()) { return }
try {
const data = { ...this.model }
const res = await this.$api.$post(`resource`, data)
this.$emit('input', res)
} catch (e) {
this.checkServerErrors(e)
// you can handle errors that are not 422
this.$emit('error', e)
}
}
}
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80