-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathComposition.vue
108 lines (94 loc) · 2.56 KB
/
Composition.vue
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<template>
<div>
<form @submit="finalForm.handleSubmit">
<div>
<input
:name="emailField.name"
:value="emailField.value"
v-on="emailField.events"
type="email"
/>
<span
class="error"
v-if="emailField.touched && emailField.error">
{{ emailField.error }}
</span>
</div>
<div>
<input
:name="passwordField.name"
:value="passwordField.value"
v-on="passwordField.events"
type="password"
/>
<span
class="error"
v-if="passwordField.touched && passwordField.error">
{{ passwordField.error }}
</span>
</div>
<div>
<input
:name="confirmPasswordField.name"
:value="confirmPasswordField.value"
v-on="confirmPasswordField.events"
type="password"
/>
<span
class="error"
v-if="confirmPasswordField.touched && confirmPasswordField.error">
{{ confirmPasswordField.error }}
</span>
</div>
<button type="submit" :disabled="formState.submitting || null">
{{ formState.submitting ? 'Submitting' : 'Submit' }}
</button>
</form>
<pre v-if="formState"><code>form state:<br><br>{{ JSON.stringify(formState, null, 2) }}</code></pre>
</div>
</template>
<script setup>
import { useForm, useField } from 'vue-final-form'
function sleep(timeout) {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, timeout)
})
}
const required = v =>
v ? null : 'This field is required!'
const matchedPassword = (value, values) =>
value === values.password ? null : 'Mismatched password!'
const range = (min, max) =>
value =>
value && value.length >= min && value.length <= max ? null : `Password should be between length ${min} and ${max}`
const noSpecialChars = (v) =>
/[!@#$%^&*()]/.test(v) ? 'Please do not use special chars' : null
const initialValues = {
email: 'egoist@boring.com',
}
const handleSubmit = async state => {
await sleep(2000)
console.log(state)
}
const { finalForm, formState } = useForm({
initialValues,
onSubmit: handleSubmit,
})
const { fieldState: emailField } = useField({
finalForm,
name: 'email',
validate: required,
})
const { fieldState: passwordField } = useField({
finalForm,
name: 'password',
validate: [range(6, 20), noSpecialChars],
})
const { fieldState: confirmPasswordField } = useField({
finalForm,
name: 'confirmPassword',
validate: matchedPassword,
})
</script>