/
CreateAccount.vue
126 lines (96 loc) · 4.09 KB
/
CreateAccount.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<script setup>
import { ref, reactive, watch } from 'vue'
import { onMounted, onUpdated } from 'vue'
const usernameRef = ref(null)
const username = ref('')
const password = ref('')
const password2 = ref('')
const disabled = ref(true)
const validUsername = ref(false)
const validPassword = ref(false)
const passwordsMatch = ref(false)
const usernameErrorRef = ref(null)
const passwordErrorRef = ref(null)
const password2ErrorRef = ref(null)
// watch element ref - similar to onMounted - not watching value of input
// for some reason disabled when ref function is installed
let count = 0
watch(usernameRef, () => {
console.log('watching usernameRef')
console.log(usernameRef.value)
console.log(count++)
usernameRef.value.value = "rmcgregor"
})
// called when anything in component changes - including onMounted
// if used in input element, must have v-model set
function componentUpdate(elm) {
console.log("password changed")
console.log(elm.value)
console.log(passwordErrorRef.value)
if (passwordErrorRef.value != null && elm.value.length > 0) {
validPassword.value = (elm.value.length >= 8)
passwordErrorRef.value.innerHTML = (validPassword.value) ?
" " : "Minimum length: 8 characters"
}
}
//watch v-model input.value ref (called when string changes)
watch(username, () => {
validUsername.value = (username.value.length >= 4)
usernameErrorRef.value.innerHTML = (validUsername.value) ?
" " : "Minimum length: 4 characters"
usernameRef.value.setAttribute("data", "hello")
})
// watch array of refs
watch([password, password2], () => {
passwordsMatch.value = (password.value === password2.value)
password2ErrorRef.value.innerHTML = (passwordsMatch.value || password2.value.length == 0) ?
" " : "Passwords do not match"
})
// watch array of refs
watch([validUsername, validPassword, passwordsMatch], async () => {
disabled.value = !(validUsername.value && validPassword.value && passwordsMatch.value)
console.log(disabled.value)
if (!disabled.value) usernameRef.value.value = ''
})
function submit() {
let mssg = username.value + ", " + password.value
console.log(mssg)
}
onMounted(() => {
usernameRef.value.focus();
})
onUpdated(() => {
//console.log(usernameRef.value.value)
})
</script>
<template>
<div class="container">
<form class="rounded border border-primary border-2 border-opacity-25 py-3 px-5">
<fieldset class="d-flex flex-column">
<legend>Create Account</legend>
<div class="form-floating mb-2">
<input ref="usernameRef" v-model="username" id="username" type="text" class="form-control" />
<label for="username" class="form-label">Username</label>
<small ref="usernameErrorRef" class="text-danger"> </small>
</div>
<div class="form-floating mb-2">
<input :ref="componentUpdate" v-model="password" id="password" type="password"
class="form-control" />
<label for="password" class="form-label">Password</label>
<small ref="passwordErrorRef" class="text-danger"> </small>
</div>
<div class="form-floating mb-2">
<input v-model="password2" id="password2" type="password" class="form-control" />
<label for="password2" class="form-label">Reenter password</label>
<small ref="password2ErrorRef" class="text-danger"> </small>
</div>
<div>
<button @click="submit" class="btn btn-primary" type="button"
v-bind:disabled="disabled">Create</button>
</div>
</fieldset>
</form>
</div>
</template>
<style>
</style>