Skip to content

Commit db25421

Browse files
committed
apply form validation to our application
1 parent b43b76a commit db25421

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"nprogress": "^0.2.0",
1919
"vue": "^2.5.2",
2020
"vue-router": "^3.0.1",
21+
"vuelidate": "^0.7.4",
2122
"vuex": "^3.0.1"
2223
},
2324
"devDependencies": {

src/main.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import App from './App'
66
import router from './router'
77
import store from '@/store'
88
import AppDate from '@/components/AppDate'
9+
import vuelidate from 'vuelidate'
910

11+
Vue.use(vuelidate)
1012
Vue.component('AppDate', AppDate)
1113

1214
Vue.config.productionTip = false

src/pages/PageRegister.vue

+64-5
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,59 @@
77

88
<div class="form-group">
99
<label for="name">Full Name</label>
10-
<input v-model="form.name" id="name" type="text" class="form-input">
10+
<input
11+
v-model="form.name"
12+
@blur="$v.form.name.$touch()"
13+
id="name" type="text" class="form-input">
14+
<template v-if="$v.form.name.$error">
15+
<span v-if="!$v.form.name.required" class="form-error">This field is required</span>
16+
</template>
1117
</div>
1218

1319
<div class="form-group">
1420
<label for="username">Username</label>
15-
<input v-model="form.username" id="username" type="text" class="form-input">
21+
<input
22+
v-model="form.username"
23+
@blur="$v.form.username.$touch()"
24+
id="username" type="text" class="form-input">
25+
<template v-if="$v.form.username.$error">
26+
<span v-if="!$v.form.username.required" class="form-error">This field is required</span>
27+
</template>
1628
</div>
1729

1830
<div class="form-group">
1931
<label for="email">Email</label>
20-
<input v-model="form.email" id="email" type="email" class="form-input">
32+
<input
33+
v-model="form.email"
34+
@blur="$v.form.email.$touch()"
35+
id="email" class="form-input">
36+
<template v-if="$v.form.email.$error">
37+
<span v-if="!$v.form.email.required" class="form-error">This field is required</span>
38+
<span v-else-if="!$v.form.email.email" class="form-error">This in not a valid email address</span>
39+
</template>
2140
</div>
2241

2342
<div class="form-group">
2443
<label for="password">Password</label>
25-
<input v-model="form.password" id="password" type="password" class="form-input">
44+
<input
45+
v-model="form.password"
46+
@blur="$v.form.password.$touch()"
47+
id="password" type="password" class="form-input">
48+
<template v-if="$v.form.password.$error">
49+
<span v-if="!$v.form.password.required" class="form-error">This field is required</span>
50+
<span v-if="!$v.form.password.minLength" class="form-error">The password must be at least 6 characters long</span>
51+
</template>
2652
</div>
2753

2854
<div class="form-group">
2955
<label for="avatar">Avatar</label>
30-
<input v-model="form.avatar" id="avatar" type="text" class="form-input">
56+
<input
57+
v-model="form.avatar"
58+
@blur="$v.form.avatar.$touch()"
59+
id="avatar" type="text" class="form-input">
60+
<template v-if="$v.form.avatar.$error">
61+
62+
</template>
3163
</div>
3264

3365
<div class="form-actions">
@@ -43,6 +75,7 @@
4375
</template>
4476

4577
<script>
78+
import {required, email, minLength} from 'vuelidate/lib/validators'
4679
export default {
4780
data () {
4881
return {
@@ -56,8 +89,34 @@
5689
}
5790
},
5891
92+
validations: {
93+
form: {
94+
name: {
95+
required
96+
},
97+
username: {
98+
required
99+
},
100+
email: {
101+
required,
102+
email
103+
},
104+
password: {
105+
required,
106+
minLength: minLength(6)
107+
},
108+
avatar: {
109+
110+
}
111+
}
112+
},
113+
59114
methods: {
60115
register () {
116+
this.$v.form.$touch()
117+
if (this.$v.form.$invalid) {
118+
return
119+
}
61120
this.$store.dispatch('auth/registerUserWithEmailAndPassword', this.form)
62121
.then(() => this.successRedirect())
63122
},

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -5101,6 +5101,10 @@ vue@^2.5.2:
51015101
version "2.5.2"
51025102
resolved "https://registry.yarnpkg.com/vue/-/vue-2.5.2.tgz#fd367a87bae7535e47f9dc5c9ec3b496e5feb5a4"
51035103

5104+
vuelidate@^0.7.4:
5105+
version "0.7.4"
5106+
resolved "https://registry.yarnpkg.com/vuelidate/-/vuelidate-0.7.4.tgz#5a0e54be09ac0192f1aa3387d74b92e0945bf8aa"
5107+
51045108
vuex@^3.0.1:
51055109
version "3.0.1"
51065110
resolved "https://registry.yarnpkg.com/vuex/-/vuex-3.0.1.tgz#e761352ebe0af537d4bb755a9b9dc4be3df7efd2"

0 commit comments

Comments
 (0)