forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationForm.vue
More file actions
98 lines (83 loc) · 2.08 KB
/
ValidationForm.vue
File metadata and controls
98 lines (83 loc) · 2.08 KB
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
<template>
<div class="q-pa-md" style="max-width: 300px">
<form @submit.prevent.stop="onSubmit" @reset.prevent.stop="onReset" class="q-gutter-md">
<q-input
ref="nameRef"
filled
v-model="name"
label="Your name *"
hint="Name and surname"
lazy-rules
:rules="nameRules"
/>
<q-input
ref="ageRef"
filled
type="number"
v-model="age"
label="Your age *"
lazy-rules
:rules="ageRules"
/>
<q-toggle v-model="accept" label="I accept the license and terms" />
<div>
<q-btn label="Submit" type="submit" color="primary" />
<q-btn label="Reset" type="reset" color="primary" flat class="q-ml-sm" />
</div>
</form>
</div>
</template>
<script>
import { useQuasar } from 'quasar'
import { ref } from 'vue'
export default {
setup () {
const $q = useQuasar()
const name = ref(null)
const nameRef = ref(null)
const age = ref(null)
const ageRef = ref(null)
const accept = ref(false)
return {
name,
nameRef,
nameRules: [
val => (val && val.length > 0) || 'Please type something'
],
age,
ageRef,
ageRules: [
val => (val !== null && val !== '') || 'Please type your age',
val => (val > 0 && val < 100) || 'Please type a real age'
],
accept,
onSubmit () {
nameRef.value.validate()
ageRef.value.validate()
if (nameRef.value.hasError || ageRef.value.hasError) {
// form has error
}
else if (accept.value !== true) {
$q.notify({
color: 'negative',
message: 'You need to accept the license and terms first'
})
}
else {
$q.notify({
icon: 'done',
color: 'positive',
message: 'Submitted'
})
}
},
onReset () {
name.value = null
age.value = null
nameRef.value.resetValidation()
ageRef.value.resetValidation()
}
}
}
}
</script>